Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display error message of jquery dropzone

Tags:

I use dropzone with CI, i don't know how to display error message and custom message when upload false, this is my script

Dropzone.autoDiscover = false;         try {             var myDropzone = new Dropzone("#adminform" , {                 paramName: "filename", // The name that will be used to transfer the file                 maxFilesize: 0.5, // MB                 url: window.location.href,                 addRemoveLinks : true,                 dictDefaultMessage :                 '<span class="bigger-150 bolder"><i class="ace-icon fa fa-caret-right red"></i> Drop files</span> to upload \                 <span class="smaller-80 grey">(or click)</span> <br /> \                 <i class="upload-icon ace-icon fa fa-cloud-upload blue fa-3x"></i>',                 dictResponseError: 'Error while uploading file!',                  //change the previewTemplate to use Bootstrap progress bars                 previewTemplate: "<div class=\"dz-preview dz-file-preview\">\n  <div class=\"dz-details\">\n    <div class=\"dz-filename\"><span data-dz-name></span></div>\n    <div class=\"dz-size\" data-dz-size></div>\n    <img data-dz-thumbnail />\n  </div>\n  <div class=\"progress progress-small progress-striped active\"><div class=\"progress-bar progress-bar-success\" data-dz-uploadprogress></div></div>\n  <div class=\"dz-success-mark\"><span></span></div>\n  <div class=\"dz-error-mark\"><span></span></div>\n  <div class=\"dz-error-message\"><span data-dz-errormessage></span></div>\n</div>",             });         }          catch(e) {             alert('Dropzone does not support older browsers!');         } 

And PHP return 400:

$this->output->set_header("HTTP/1.0 400 Bad Request"); 

But when i hover image it's display [object Object] but message is:

dictResponseError: 'Error while uploading file!'

enter image description here

like image 304
Code Metal Avatar asked Nov 20 '14 02:11

Code Metal


People also ask

How do I show dropzone error?

I found using the PHP 400 header at the beginning of the file allowed all my PHP errors to appear on the file's preview in Dropzone. This was really helpful as I'm running a few database queries when uploads complete. I recommend just always sending an error header on your dev server to make debugging easier.

Does dropzone require jQuery?

The files are uploaded via AJAX. It does not require any external libraries such as jQuery. dropzone.

What is dropzone in JavaScript?

Dropzone. js is 'a light weight JavaScript library that turns an HTML element into a "dropzone"'. Users can drag and drop a file onto an area of the page, uploading to a server. If you would like to read more how all of this works skim through the Dropzone.


2 Answers

For anyone in need:

You can return a response message from the server using echo. Then in the js code add an error event handler

PHP

header("HTTP/1.0 400 Bad Request"); echo "Ups error message"; 

JS

this.on('error', function(file, response) {     $(file.previewElement).find('.dz-error-message').text(response); }); 
like image 154
Alwin Kesler Avatar answered Sep 18 '22 12:09

Alwin Kesler


For me this code finally worked, used as a dropzone option:

error: function(file, message) {        $(file.previewElement).addClass("dz-error").find('.dz-error-message').text(message.Message);      }

I used message.Message since the ASP.net WebAPI returns a JSON object, but not with the required "error" key.

like image 20
Fabian von Ellerts Avatar answered Sep 20 '22 12:09

Fabian von Ellerts