Reputation: 2685
I'm trying to figure out a way how to rename file after upload using dropzone.js so I could later delete it just by sending correct name.
What I have right now:
this.on("success", function(file, responseText) {
console.log(responseText); // responseText contains actual file name after server modifications
});
addRemoveLinks: true,
this.on("removedfile", function(file) {
var name = file.name;
$.ajax({
type: 'POST',
url: 'delete_file.html',
data: {
'file-name': name,
},
});
});
As you can see in my ajax data I am sending initial file name, not the one that is actually on the server (e.g. if there are multiple same named files server will rename them). I have been thinking of changing previewElement name on success:
file.previewElement.querySelector(".name").textContent = responseText;
and then refer to this in ajax, but it doesn't look like an elegant approach.
Other alternative would be to create a map with <file, new_name> mapping, but I'm not sure if that's not an overkill.
How would you recommend accessing new file name after upload?
Upvotes: 2
Views: 358
Reputation: 2685
The cleanest option I came up with is using file.xhr.response
value which hold the new name instead of file.name
Upvotes: 1