【发布时间】:2017-05-01 19:00:35
【问题描述】:
我正在使用 DropZone JS 和 MVC。保存图像的ActionMethod 正在使用try catch。现在,如果发生错误,我需要从ActionMethod 返回什么,以便前端将拾取它并向用户显示错误标记,而不是显示一切都成功了。
它是用DropZone 内置的还是我需要将它绑定到诸如完成之类的事件?如果有,怎么做?
DropZone JS 完成事件的示例
this.on("complete", function (file, response) {
// If an error has occurred, mark the item as failed
if (response.code != 200){
}
// If it went through successful, show that to the user
if (response.code == 200){
}
});
如果这可行,在MVC 我可以返回HttStatusCodeResult,例如
return new HttpStatusCodeResult(HttpStatusCode.BadRequest) 和 return new HttpStatusCodeResult(HttpStatusCode.Ok)
已更新 - ActionMethod
[HttpPost]
public ActionResult SaveImages()
{
bool isSavedSuccessfully = true;
string fName = "";
try
{
foreach (string fileName in Request.Files)
{
HttpPostedFileBase file = Request.Files[fileName];
if (HttpPostedFileBaseExtensions.IsImage(file))
{
//Save file content goes here
fName = file.FileName;
if (file != null && file.ContentLength > 0)
{
var originalDirectory = new DirectoryInfo(string.Format("{0}Images\\", Server.MapPath(@"\")));
string pathString = Path.Combine(originalDirectory.ToString(), "Temp");
var fileName1 = Path.GetFileName(file.FileName);
bool isExists = Directory.Exists(pathString);
if (!isExists)
Directory.CreateDirectory(pathString);
var path = string.Format("{0}\\{1}", pathString, file.FileName);
file.SaveAs(path);
_testRepository.EditMainPicture("test", pathString, "imageText", 1);
}
}
}
}
catch (Exception ex)
{
// TODO Add error logging!!
isSavedSuccessfully = false;
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
return new HttpStatusCodeResult(HttpStatusCode.Ok);
}
由于某种原因,如果在此方法中出现任何其他问题,DropZone 将不会接收它,它会标记为文件已成功上传。如果ActionMethod 内出现任何故障,我希望它显示错误
【问题讨论】:
-
收听error event
dropzone.on("error", function(file, data){...})。你的 mvc 操作代码在哪里? -
Jasen,添加您的评论作为答案,以便我为您标记。它确实有效,但我还必须添加
success事件。
标签: asp.net-mvc model-view-controller asp.net-mvc-5 dropzone.js