How to display existing files on server using DropzoneJS and Go

Written by kataras | Published 2017/10/04
Tech Story Tags: javascript | golang | go | programming | web-development

TLDRvia the TL;DR App

This is the part 2 of 2 in DropzoneJS + Go series.

DropzoneJS + Go: How to display existing files on server

In this tutorial, we will show you how to display existing files on the server when using DropzoneJS and Go. This tutorial is based on How to build a file upload form using DropzoneJS and Go. Make sure you have read it before proceeding to content in this tutorial.

Table Of Content

  • Preparation
  • Modify the Server side
  • Modify the Client side
  • References
  • The End

Preparation

Install the go package “github.com/nfnt/resize” with go get github.com/nfnt/resize, we need it to create thumbnails.

In previous tutorial. We have already set up a proper working DropzoneJs upload form. There is no additional file needed for this tutorial. What we need to do is to make some modifications to file below:

  1. main.go
  2. views/upload.html

Let us get started!

Modify the Server side

In previous tutorial. All “/upload” does is to store uploaded files to the server directory “./public/uploads”. So we need to add a piece of code to retrieve stored files’ information (name and size), and return it in JSON format.

Copy the content below to “main.go”. Read comments for details.

Modify the Client side

Copy content below to “./views/upload.html”. We will go through modifications individually.

  1. We added Jquery library into our page. This actually not for DropzoneJs directly. We are using Jquery’s ajax function $.get only. You will see below
  2. We added an ID element (my-dropzone) to the form. This is needed because we need to pass configuration values to Dropzone. And to do it, we must have an ID reference of it. So that we can configure it by assigning values to Dropzone.options.myDropzone. A lot of people face confusion when configuring Dropzone. To put it in a simple way. Do not take Dropzone as a Jquery plugin, it has its own syntax and you need to follow it.
  3. This starts the main part of modification. What we did here is to pass a function to listen to Dropzone’s init event. This event is called when Dropzone is initialized.
  4. Retrieve files details from the new “/uploads” via ajax.
  5. Create mockFile using values from server. mockFile is simply JavaScript objects with properties of name and size. Then we call Dropzone’s addedfile and thumbnail functions explicitly to put existing files to Dropzone upload area and generate its thumbnail.

Running the server

Open the terminal at the current project’s directory and execute:

$ go run main.goNow listening on: http://localhost:8080Application started. Press CTRL+C to shut down.

If you have done it successfully. Now go and upload some images and reload the upload page. Already uploaded files should auto display in Dropzone area.

References

The end

Hopefully this simple tutorial helped you with your development. If you like my post, please follow me on Twitter and help spread the word. I need your support to continue.

Gerasimos Maropoulos (@MakisMaropoulos) | Twitter_The latest Tweets from Gerasimos Maropoulos (@MakisMaropoulos). That #golang guy. Self-taught Developer with 10+ years…_twitter.com

I like the visual effects when I click the clap button more than once, do you? It’s simple: just click the clap button. If you feel strongly, click it more (or just hold it down).


Published by HackerNoon on 2017/10/04