如何使用Dropzone.js在服务器错误响应后触发警报

时间:2015-10-14 22:50:08

标签: javascript php json dropzone.js

我想在服务器返回错误时从dropzone.js触发javascript警报。 (在json中)。

这是我的功能/方法。它似乎工作正常。

public function file_upload($account_id,$bid_id) {
    $path = FMREPO . "/account-" . $account_id . "/bid-project-" .$bid_id . "/";
    if (file_exists ( $path )){
        if (!empty($_FILES)) {
            $tempFile = $_FILES['file']['tmp_name'];
            $fileName = $_FILES['file']['name'];
            $targetPath = $path . "diagram-" . $bid_id . "-";
            $targetFile = $targetPath . $fileName ;
            move_uploaded_file($tempFile, $targetFile);
                // $this->load->database(); // load database
                // $this->db->insert('file_table',array('file_name' => $fileName));
        }
    } else{
        header('HTTP/1.1 500 Internal Server Directory Not Found');
        header('Content-Type: application/json; charset=UTF-8');
        die(json_encode(array('error' => 'File Could Not Be Saved')));
    }   
}

这是我的Dropzone选项。这是我不知道该怎么做或让它发挥作用的部分。 "错误:"是我添加但它总是触发警报,即使文件上传成功。我需要做什么?

Dropzone.options.diagramDropzone = {
paramName: "file", // The name that will be used to transfer the file
maxFilesize: 6, // MB
maxFiles: 2,
thumbnailWidth: 100,
thumbnailHeight: 100,
error: function(){
    alert('error');
},
init: function() {
    this.on("maxfilesexceeded", function(file){
        alert("No more files please!");
        console.log(file);
        this.removeFile(file);
    });
}

}

编辑:我刚刚意识到这一点

error: function(){ //alert here },

应该是

error: function(response){ //alert here },

所以现在就行了!但是alert(response)我得到[对象]。所以现在的问题是如何提醒我的字符串?

1 个答案:

答案 0 :(得分:1)

您可以在init选项中设置警报,如下所示:

init: function() {
    this.on("maxfilesexceeded", function(file){
        alert("No more files please!");
        console.log(file);
        this.removeFile(file);
    }),
    this.on("error", function(file, errorMessage, xhr) {
        alert(errorMessage);
    ])
}