简体   繁体   中英

DropZone.js determine when successful upload

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.

Here is my 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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM