如果出现错误,我应该从 MVC 控制器 return 做什么,以确保 DropZone 能够拾取它
What should I return from MVC controller in case of an error to make sure DropZone will pick it up
我正在使用 DropZone JS
和 MVC
。保存图像的 ActionMethod
正在使用 try catch。现在,如果出现错误,我需要从 ActionMethod
中 return 做什么,以便前端将拾取它并向用户显示错误标记,而不是显示一切都成功了。
它是用 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
我可以 return 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
An error occured. Receives the errorMessage as second parameter and if the error was due to the XMLHttpRequest the xhr object as third.
示例
dropzone.on("error", function(file, errorMesage, xhr) { ... });
我正在使用 DropZone JS
和 MVC
。保存图像的 ActionMethod
正在使用 try catch。现在,如果出现错误,我需要从 ActionMethod
中 return 做什么,以便前端将拾取它并向用户显示错误标记,而不是显示一切都成功了。
它是用 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
我可以 return 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
An error occured. Receives the errorMessage as second parameter and if the error was due to the XMLHttpRequest the xhr object as third.
示例
dropzone.on("error", function(file, errorMesage, xhr) { ... });