[英]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. 我正在使用Dropzone.js上载Excel文件,然后将其内容导入到数据库中的表中。
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. 目前,我的C#中有一些方法可以检查正在上传的文件,以确保该文件有效(检查标头行)并且可以将其导入数据库。
The validation works fine, as does DropZone.js in theory. 从理论上讲,验证工作正常,就像DropZone.js一样。 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都将始终显示“ tick / check”标记-通知用户操作已成功完成。
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: 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. 因此,Upload方法返回一个JSON对象。 And I want DropZone to determine whether the save/import was successful, based on a value from the JSON.
我希望DropZone根据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. 与其尝试解析JSON响应并处理错误客户端,不如让您的服务器对此负责。
Specifically: have your server return something other than a successful HTTP 200
response when an upload fails. 具体来说:上载失败时,让服务器返回
HTTP 200
响应成功以外的内容。 DropZone will treat an upload as failed if it receives a 4xx
or 5xx
response from the server. 如果DropZone从服务器收到
4xx
或5xx
响应,它将把上载视为失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.