5

I've been trying to rename the filename before the upload in dropzone.js but I'm not able to make it work. This is my configuration:

Dropzone.autoDiscover = false;
Dropzone.options.myAwesomeDropzone = {
    url: url,
    paramName: "image",
    dictDefaultMessage: 'Selecciona tus archivos..',
    dictRemoveFile: "Eliminar",
    dictCancelUpload: "Cancelar carga",
    addRemoveLinks: true,
    uploadMultiple: false,
    renameFile: function (file) {
        console.log(file.name);
        file.name = new Date().getTime() + '_' + file.name;
    },
    new Dropzone("div#my-awesome-dropzone");

When it upload nothing is even showing in the js console and the file name is still the same

Has someone gone through this?

I tried this solution: Dropzone.js - How to change file name before uploading to folder

Marco Herrarte
  • 1,290
  • 3
  • 17
  • 37

1 Answers1

13

The function in renameFile has to return the new name. It's not well explained int the documentation, tested with dropzone.js (version 5.2).

The code inside the renameFile option should look like:

renameFile: function (file) {
    let newName = new Date().getTime() + '_' + file.name;
    return newName;
}
wallek876
  • 3,001
  • 2
  • 17
  • 28
  • @MarcoHerrarte yeah I see you are right, this option is not well documented, I took a look at the source code and this function is expected to return the modified name, I'll update the answer. – wallek876 Apr 27 '18 at 20:49
  • 1
    man, I tried that yesterday and it didn't worked at all, just tried now again and still not working, unable to rename the file using this option. Have you tried on your own? – Marco Herrarte Apr 27 '18 at 21:27
  • This blog saved my life.. https://www.sitepoint.com/community/t/how-to-not-let-the-file-name-be-overwritten-in-dropzone-js/283728/7 – Marco Herrarte Apr 27 '18 at 21:47
  • I was using an older version of the framework, and your answer is correct. – Marco Herrarte Apr 27 '18 at 21:48
  • @MarcoHerrarte sorry I forgot to mention, I've tested with the last version, 5.2 I believe, for the comments in the source code seems that this part of the library is a work in progress, so it's possible that this behavior has changed in the last different versions. – wallek876 Apr 28 '18 at 05:32