How to upload a smaller version and the original image in dropzone.js?

by mallory_cormier , in category: Javascript , a month ago

How to upload a smaller version and the original image in dropzone.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by elise_daugherty , 17 days ago

@mallory_cormier 

To upload both a smaller version and the original image in Dropzone.js, you can follow these steps:

  1. Create a Dropzone instance on your webpage:
1
<form action="/upload" class="dropzone" id="myDropzone"></form>


  1. Configure your Dropzone instance to accept multiple files and create your own custom thumbnail event handler to generate a smaller version of the image before uploading:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Dropzone.options.myDropzone = {
  paramName: "file", // The name that will be used to transfer the file to the server
  maxFilesize: 2, // MB
  accept: function(file, done) {
    // Validate the file before adding it to the Dropzone
    done();
  },
  init: function() {
    this.on("addedfile", function(file) {
      // Create a smaller version of the image
      var reader = new FileReader();
      reader.onload = function(event) {
        var img = new Image();
        img.src = event.target.result;

        img.onload = function() {
          var canvas = document.createElement('canvas');
          var ctx = canvas.getContext('2d');
          canvas.width = 100; // Set the width of the thumbnail
          canvas.height = 100; // Set the height of the thumbnail
          ctx.drawImage(img, 0, 0, 100, 100);
          var dataURL = canvas.toDataURL('image/jpeg');

          // Attach the smaller version of the image to the file
          file.thumbnail = dataURL;
          file.previewElement.addEventListener('click', function() {
            // Open the smaller version of the image in a new window
            window.open(dataURL);
          }, false);
        };
      };
      reader.readAsDataURL(file);
    });
  }
};


  1. Handle the file upload on your server-side script (e.g. Node.js, PHP, etc.) and save both the original image and the smaller version:


For example, using Node.js and the multer middleware:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
var express = require('express');
var multer = require('multer');

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'uploads/')
  },
  filename: function (req, file, cb) {
    cb(null, file.originalname)
  }
});

var upload = multer({ storage: storage });

var app = express();

app.post('/upload', upload.single('file'), function (req, res, next) {
  // Save the original image
  // Code to save the smaller version of the image
  res.send('File uploaded successfully');
});

app.listen(3000, function () {
  console.log('Server listening on port 3000');
});


By following these steps, you can upload both a smaller version and the original image in Dropzone.js.