Skip to content
This repository was archived by the owner on May 25, 2023. It is now read-only.

support to modify filename before sending file to server #3497

Closed
wants to merge 1 commit into from
Closed

support to modify filename before sending file to server #3497

wants to merge 1 commit into from

Conversation

sucotronic
Copy link

@sucotronic sucotronic commented Nov 29, 2017

With this change, you can alter the names of the files being added. I need this in order to support IOS file upload, because in that environment all the images selected are called the same: https://stackoverflow.com/questions/21135037/html-upload-on-iphone-ios-filename-is-always-same-image-jpg-image-png

You can change the name the name of the files after applying this mod the following way:

add: function (e, data) {
  for(var i=0; i<data.files.length ;i++){
    var randomNumber = Math.floor(Math.random()*1000);
    var seconds = Math.floor(new Date().getTime() / 1000);
    var newName = randomNumber+'_'+seconds+'_'+data.files[i].name;
    data.files[i].Aname = newName;
  }
  ...
}

@blueimp blueimp closed this in c545ea5 Dec 1, 2017
@blueimp
Copy link
Owner

blueimp commented Dec 1, 2017

Thanks for your contribution, @sucotronic

An option to supply an alternative file name has already been added 4 years ago:
0decc74

However I've added support for the uploadName property for blob and non-multipart uploads.

@sucotronic
Copy link
Author

Thank you so much. I've reverted my modifications and replaced the property name with the uploadName one, and everything works fine.

Anyway, I've not been able to find any reference to the uploadName property in the library documentation, so it'll be nice to add some kind of reference for other devs ;)

@sucotronic sucotronic deleted the custom_name_support branch December 11, 2017 11:19
@blueimp
Copy link
Owner

blueimp commented Dec 11, 2017

Agreed regarding documentation.
Any suggestion what would be the best place to add this?

@sucotronic
Copy link
Author

After the code example that follows this text:

One special callback is the add callback, as it provides a submit method for the data argument, that will start the file upload:

I'd just put some example code like this:

add: function (e, data) {
    // Change file names before sending to the server
    for(var i=0; i<data.files.length ;i++){
        var randomNumber = Math.floor(Math.random()*1000);
        var newName = randomNumber+'_'+data.files[i].name;
        data.files[i].uploadName = newName;
    }
	data.submit();
}

@blueimp
Copy link
Owner

blueimp commented Dec 19, 2017

Thanks, I've added a section here:
https://github.com/blueimp/jQuery-File-Upload/wiki/API#how-to-provide-alternative-file-names

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants