Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
109
rated 0 times [  111] [ 2]  / answers: 1 / hits: 20365  / 10 Years ago, sat, may 24, 2014, 12:00:00

I need to check if a file has a valid MIME type, if the file size is ok and if its dimensions are ok, then upload file.



So when everything is OK, I can use:



complete: function(file){
// do something here.
}


but what if the size of file was invalid? In my PHP script I return an error message:



return json_encode(['error' => 'size is invalid']);


OR



return Response::json(['error' => 'size is invalid'], 500 ];
// this is Laravel 4 syntax. returns a json array and 500 as status code.


but how can I handle that error in DropzoneJS?



I tried adding a second parameter to the complete() function but it's not working.



complete: function(file, response){
console.log( response ); // this does not work.
}

More From » dropzone.js

 Answers
170

To get the response after the file was submitted to server use this in DropzoneJS:



success: function(file, response) {
alert(response);
}


And to validate the file before uploading it use this:



complete: function(file) {
if (file.size > 3.5*1024*1024) {
alert(File was Larger than 3.5Mb!);
return false;
}

if(!file.type.match('image.*')) {
alert(Upload Image Only!);
return false;
}
}


If your server is returning response in JSON, you'll need to use JSON.parse before alerting it.



Hope it'll help you! Cheers! :)


[#70865] Thursday, May 22, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
andreguym

Total Points: 125
Total Questions: 112
Total Answers: 103

Location: Wallis and Futuna
Member since Tue, Mar 30, 2021
3 Years ago
;