Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropzone JS Change Filename Before Upload

I'm using Dropzone.js to handle uploading of files. I would really like to be able to modify the original name of the file before uploading it to S3. It would be nice to just be able to use dropzone's processing file hook like shown below, but it appears none of the changes I make to this file object persist...

myDropone.on('processingfile', function(file) {
  console.log(file.name) // 'Randy.png'
  file.name = 'my-custom-name.png';
  console.log(file.name) // 'Randy.png'
});

Even when trying to modify this File object in the console changes do not persist. I'm losing my mind...

enter image description here

Does anyone see what I'm missing here? Or is there a better way to modify the name of a file before uploading it with Dropzone?

like image 686
Cumulo Nimbus Avatar asked Mar 02 '16 20:03

Cumulo Nimbus


People also ask

How do I change a file name in Dropzone?

There is a renameFile option in dropzone. js that will let you change the uploaded file name. For older versions of Dropzone the option name is renameFilename .

How does dropzone js work?

Dropzone. js is 'a light weight JavaScript library that turns an HTML element into a "dropzone"'. Users can drag and drop a file onto an area of the page, uploading to a server. If you would like to read more how all of this works skim through the Dropzone.


1 Answers

File is a HTML 5 file object, and some of it's properties is read-only as you can see here

But, you can set a new property for your file object like:

myDropone.on("sending", function(file) {
    file.myCustomName = "my-new-name" + file.name;
    console.log(file.myCustomName);
});

Edit: Also, the documentation says, the recommended way to send aditional params in the body of the post action is:

myDropzone.on("sending", function(file, xhr, formData) {
     // Will send the filesize along with the file as POST data.
     formData.append("filesize", file.size);
     formData.append("fileName", "myName");
});

Hope it helps.

like image 75
Jaime Yule Avatar answered Oct 02 '22 15:10

Jaime Yule