如何显示jquery dropzone的错误消息

时间:2014-11-20 02:24:13

标签: dropzone.js

我使用dropzone和CI,我不知道如何在上传时显示错误消息和自定义消息,这是我的脚本

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!');
        }

PHP返回400:

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

但是当我悬停图像时,它显示[object Object]但消息是:

  

dictResponseError:&#39;上传文件时出错!&#39;

enter image description here

3 个答案:

答案 0 :(得分:19)

对于有需要的人:

您可以使用echo从服务器返回响应消息。然后在js代码中添加一个错误事件处理程序

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);
});

答案 1 :(得分:7)

您只需通过PHP文件回显来自服务器的消息

if($file_uploaded == true)
{

   //perform operations on valid upload

} else {

  //upload failed, echo back negative response to dropzone.js
  $this->output->set_header("HTTP/1.0 400 Bad Request");
  echo "Error uploading file";

}

虽然您的HTML文件可能如下所示:

<script type="text/javascript">
    Dropzone.options.myAwesomeDropzone = {
        paramName: "icon_image", // The name that will be used to transfer the file
        maxFilesize: 2, // MB
        init: function() {
            this.on("error", function(file, response) {
                // do stuff here.
                alert(response);

            });

        }
    };
</script>

希望有所帮助:)

答案 2 :(得分:7)

对我来说,这段代码最终起作用,用作dropzone选项:

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

我使用了message.Message,因为ASP.net WebAPI返回一个JSON对象,但没有返回所需的“错误”键。