I have a situation that I have tried to solve in various ways and it has not worked for me.
What I need is that when starting the upload of a video, I can create an ID in the database and return it to the Dropzone sending event to send it along with the other form fields. I need the ID because while the video is uploading, I have to show a preview of what was saved in the database and the user can modify the name of the video.
To create the ID in the DB I have this Ajax function:
function create() {
var result;
$.ajax({
type: 'post',
url: urlroot + 'Videos/id',
data: {},
success: function (response) {
myCallback(response);
}
});
return result;
}
function myCallback(response) {
return response;
}
And this is the sending event from Dropzone:
this.on("sending", function (file, xhr, formData) {
var idv = create();
formData.append("id", idv);
formData.append("artistas", jQuery("#artistas").val());
formData.append("artistas", jQuery("#artistas").val());
});
The problem is that the $ ajax response does not arrive before the sending event is executed, so my "idv" enters the PHP script as "undefined". I understand that it is because of the asynchronous call. I tried to somehow delay the execution of sending (with promises or setTimeOut) but it doesn't work.
I also tried to create directly in the upload PHP script, the ID in the DB and return it with "return" or with "echo", but the answer can only be received in the "success" event and I need you to be able to modify the name of the video while uploading to the server.
I also tried to access in the "sending" event the data of the file or XHR object and with console.log it shows me the .responseText, but when I want to access directly it tells me that it is empty:
console.log(file.xhr.responseText);
console.log(xhr.responseText);
I don't know what else to try, I need that ID to modify the data in the database while the file is being uploaded since the name of the video can be repeated. Maybe I should change my focus, if someone enlightens me it would be greatly appreciated.
I am attentive to your comments, and sorry about my English.