【问题标题】:What should I return from MVC controller in case of an error to make sure DropZone will pick it up如果出现错误,我应该从 MVC 控制器返回什么以确保 DropZone 将其拾取
【发布时间】:2017-05-01 19:00:35
【问题描述】:

我正在使用 DropZone JSMVC。保存图像的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


【解决方案1】:

您需要收听error event

发生错误。接收 errorMessage 作为第二个参数,如果错误是由于 XMLHttpRequest 引起的,则将 xhr 对象作为第三个参数。

例子

dropzone.on("error", function(file, errorMesage, xhr) { ... });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-23
    • 2012-10-02
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多