How to display error message of jquery dropzone

前端 未结 3 1013
时光说笑
时光说笑 2021-02-12 11:26

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;
               


        
相关标签:
3条回答
  • 2021-02-12 11:57

    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);
    });
    
    0 讨论(0)
  • 2021-02-12 12:05

    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.

    0 讨论(0)
  • 2021-02-12 12:12

    You can simply echo back the message from server via PHP file

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

    While your HTML file can look like:

    <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>
    

    Hope it helps :)

    0 讨论(0)
提交回复
热议问题