DropZone.js判断何时上传成功
DropZone.js determine when successful upload
我正在使用 Dropzone.js 上传一个 Excel 文件,然后将其内容导入到我的数据库中的 table。
我目前在我的 c# 中有一些方法可以检查正在上传的文件,以确保它有效(检查 header 行)并且可以导入到数据库中。
验证工作正常,理论上 DropZone.js 也是如此。但是,无论文件是否通过验证并被导入,DropZone 将始终显示 'tick/check' 标记 - 以通知用户操作已成功完成。
这是我的投放区:
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>
以及正在调用的 'Upload' 方法:
[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" });
}
所以上传方法返回 JSON object。我希望 DropZone 根据 JSON 中的值确定 save/import 是否成功。这可能吗?
非常感谢
与其尝试解析 JSON 响应并处理客户端的错误,我会让您的服务器对此负责。
具体来说:当上传失败时,让您的服务器 return 除了成功 HTTP 200
响应之外的其他内容。如果从服务器收到 4xx
或 5xx
响应,DropZone 会将上传视为失败。
我正在使用 Dropzone.js 上传一个 Excel 文件,然后将其内容导入到我的数据库中的 table。
我目前在我的 c# 中有一些方法可以检查正在上传的文件,以确保它有效(检查 header 行)并且可以导入到数据库中。
验证工作正常,理论上 DropZone.js 也是如此。但是,无论文件是否通过验证并被导入,DropZone 将始终显示 'tick/check' 标记 - 以通知用户操作已成功完成。
这是我的投放区:
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>
以及正在调用的 'Upload' 方法:
[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" });
}
所以上传方法返回 JSON object。我希望 DropZone 根据 JSON 中的值确定 save/import 是否成功。这可能吗?
非常感谢
与其尝试解析 JSON 响应并处理客户端的错误,我会让您的服务器对此负责。
具体来说:当上传失败时,让您的服务器 return 除了成功 HTTP 200
响应之外的其他内容。如果从服务器收到 4xx
或 5xx
响应,DropZone 会将上传视为失败。