Asp.net mvc 如果出现错误,我应该从MVC控制器返回什么,以确保DropZone将拾取它
我正在使用Asp.net mvc 如果出现错误,我应该从MVC控制器返回什么,以确保DropZone将拾取它,asp.net-mvc,model-view-controller,asp.net-mvc-5,dropzone.js,Asp.net Mvc,Model View Controller,Asp.net Mvc 5,Dropzone.js,我正在使用DropZone JS和MVC一起使用。保存图像的ActionMethod正在使用try-catch。现在,如果出现错误,我需要从ActionMethod返回什么,这样前端将拾取该错误并向用户显示错误标记,而不是显示所有操作都成功 它是与DropZone一起内置的,还是需要将它绑定到事件(如complete)上?如果是,怎么做 DropZone JScomplete事件示例 this.on("complete", function (file, response) {
DropZone JS
和MVC
一起使用。保存图像的ActionMethod
正在使用try-catch。现在,如果出现错误,我需要从ActionMethod
返回什么,这样前端将拾取该错误并向用户显示错误标记,而不是显示所有操作都成功
它是与DropZone
一起内置的,还是需要将它绑定到事件(如complete)上?如果是,怎么做
DropZone JS
complete事件示例
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
中,我可以返回httstatuscodesult
如下
返回新的HttpStatusCodeResult(HttpStatusCode.BadRequest)
和返回新的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
将不会拾取它,并将标记为文件已成功上载。我想让它显示一个错误,如果在操作方法中出现任何故障,您需要监听
[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);
}
发生了一个错误。将errorMessage作为第二个参数接收,如果错误是由XMLHttpRequest引起的,则将xhr对象作为第三个参数接收
范例
dropzone.on("error", function(file, errorMesage, xhr) { ... });
监听dropzone.on(“error”,函数(文件,数据){…})
。你的mvc操作代码在哪里?Jasen,添加你的评论作为答案,这样我可以为你标记它。它确实有效,但我还必须添加success
事件。