DropZone.js确定何时成功上传

时间:2022-11-23 21:23:09

I am using Dropzone.js to upload an Excel file, of which it's contents is then imported to a table in my database.

I currently have methods in my c# which check the file being uploaded, to make sure it is valid (checks header row) and can be imported to the database.

The validation works fine, as does DropZone.js in theory. However, no matter if the file passes validation and is imported, or not, DropZone will always show the 'tick/check' mark - to notify the user that the action has completed successfully.

我正在使用Dropzone.js上传一个Excel文件,然后将其内容导入到我的数据库中的表中。我目前在c#中有方法检查正在上传的文件,以确保它是有效的(检查标题行)并可以导入到数据库中。验证工作正常,理论上DropZone.js也是如此。但是,无论文件是否通过验证并且是否已导入,DropZone将始终显示“勾选/勾选”标记 - 以通知用户该操作已成功完成。

Here is my Dropzone:

这是我的Dropzone:

Dropzone.options.forecastDropzone = {
    init: function () {
        thisDropzone = this;
        this.on("success", function (file, Message) {
            console.log(Message.Message)
            toastr.info(Message.Message, {
                timeOut: 0, tapToDismiss: true, preventDuplicates: true
            });
        });
    },
};

HTML:

<form action="/Power/Upload" class="dropzone" id="forecastDropzone"></form>

And the 'Upload' method which is being called:

并且正在调用的“上传”方法:

[HttpPost]
    public ActionResult Upload()
    {
        string filename = "";
        string path = "";
        try
        {
            foreach (string fileName in Request.Files)
            {
                HttpPostedFileBase file = Request.Files[fileName];
                path = AppDomain.CurrentDomain.BaseDirectory + "Uploads/";
                filename = Path.GetFileName(Request.Files[fileName].FileName);
                Request.Files[fileName].SaveAs(Path.Combine(path, filename));
                ValidateExcel(path, filename);
            }
        }               
        catch
        {
            isSavedSuccessfully = false;
        }

        return Json(isSavedSuccessfully == true ? new { Message = "Successfully Saved!" } : new { Message = "Error in saving file" });

    }

So the Upload method is returning a JSON object. And I want DropZone to determine whether the save/import was successful, based on a value from the JSON. Is this possible?
Many thanks

所以Upload方法返回一个JSON对象。我希望DropZone根据JSON中的值确定保存/导入是否成功。这可能吗?非常感谢

1 个解决方案

#1


2  

Instead of trying to parse the JSON response and handle the error client side, I would make your server responsible for this.

我会让你的服务器对此负责,而不是试图解析JSON响应并处理错误客户端。

Specifically: have your server return something other than a successful HTTP 200 response when an upload fails. DropZone will treat an upload as failed if it receives a 4xx or 5xx response from the server.

具体来说:当上载失败时,让您的服务器返回除成功的HTTP 200响应之外的其他内容。如果从服务器收到4xx或5xx响应,DropZone会将上传视为失败。

#1


2  

Instead of trying to parse the JSON response and handle the error client side, I would make your server responsible for this.

我会让你的服务器对此负责,而不是试图解析JSON响应并处理错误客户端。

Specifically: have your server return something other than a successful HTTP 200 response when an upload fails. DropZone will treat an upload as failed if it receives a 4xx or 5xx response from the server.

具体来说:当上载失败时,让您的服务器返回除成功的HTTP 200响应之外的其他内容。如果从服务器收到4xx或5xx响应,DropZone会将上传视为失败。