How can I trigger an error event in DropZone using PHP?

There is an error event in dropzone.js, however I cannot find any documentation on how to call this from PHP.

I tried to send various header () responses from PHP, including 404, 500, 503, etc., but this event was not fired.

What I want to do, the server checks the mime to see if it is valid, and if it is invalid, I drop the file and, ideally, return an error so that dropzone can respond accordingly.

From the javascript side, I tried the following:

 .on("complete", function(file,response) { console.log(response); } .on("error", function(file,response) { console.log(response); } .on("success", function(file,response) { console.log(response); } 

... however, the response is undefined, even if I return JSON or plain text from a PHP script. It doesn't seem like dropzone.js supports getting a full server response, or at least doesn't raise it to a user exit. ONLY the place where I saw any link to pass the second parameter to events is here on SO in other questions that do not directly ask this question.

There should be a way to get the server response (as it was in the past with another javascript loader like jQuery POST and jqUpload, etc.). It seems rather silly that I cannot call a command to signal that the download failed - even if the file transfer is complete - because the script still needs to wait for the response. --- This means that I probably don’t notice something, so I ask for help, since their documentation shows absolutely nothing about how the server should respond - the language does not matter, however in my case I use PHP .

Thanks in advance.

+7
javascript jquery php
source share
2 answers
 <?php header('HTTP/1.1 500 Internal Server Error'); header('Content-type: text/plain'); $msg = "Your error message here."; exit($msg); ?> 

NOTE. Do not redirect the user, otherwise he will not run "exit ($ msg)"

 <script type="text/javascript"> $( document ).ready(function() { Dropzone.options.dropzone = { maxFilesize: 10, init: function() { this.on("uploadprogress", function(file, progress) { console.log("File progress", progress); }); } }; }); </script> 

Here it is!. He should work

+11
source share

If you send any non-200 response header, Dropzone will detect it as an error and fire an error(file, response) event. The error information may come as JSON and is available through response.your_msg_variable

Note that just printing response will not show anything useful, since this is an object, you can use console.log(JSON.stringify(responseText, null, 4));

It will pretty print an object up to 4 levels down

+1
source share

All Articles