簡體   English   中英

Blueimp jQuery文件上傳和我的異常

[英]Blueimp jQuery File Upload and my exception

我想將錯誤消息傳遞給Blueimp jQuery File Upload插件。 我使用ASP.NET MVC並在出現某些情況時引發我自己的異常(即文件不是真實圖像,只有圖像異常或圖像太寬等)。

                var file = Request.Files[i];
                _service.ImageValidation(file.InputStream);

    public void ImageValidation(System.IO.Stream fileStream)
    {
        Bitmap bmp = null;
        try
        {
            bmp = new Bitmap(fileStream, false);
        }
        catch
        {
            throw new NoImageException();
        }
        if (bmp.Width > bmp.Height && (bmp.Width < 1024 || bmp.Height < 768))
            throw new ImageDimensionTooSmall();

        if ((bmp.Width <= bmp.Height) && (bmp.Width < 768 || bmp.Height < 1024))
            throw new ImageDimensionTooSmall();

        fileStream.Position = 0;
    }

在客戶端,我嘗試通過以下方式捕獲錯誤:

        $('#fileupload').fileupload({
            url: '/SmartphonePhotographer/ManageFiles?ResponseID=' + ResponseID,
            error: function (e, data) {
                alert('error');
            }
        });

“數據”變量始終具有“錯誤”值。 “ e”具有許多屬性,包括statusText =“內部服務器錯誤”和responseText(帶有例外的html頁)。 問題-如何在服務器端傳遞錯誤消息以在客戶端捕獲它(也許,錯誤存在json格式,但是我在文檔中找不到它)

轉到錯誤事件,因為您在服務器端代碼中引發了異常。 因此,ajax調用獲得了500個內部錯誤響應。

您可以做的是,而不是引發異常,而是返回帶有錯誤消息的json響應。

[HttpPost]
public ActionResult SaveImage()
{
   if(IsFileNotValid())  //your method to validate the file
   {
      var customErrors = new List<string> {"File format is not good",
                                            "File size is too bib"};
      return Json(new { Status = "error", Errors = customErrors });

   }
   //Save/Process the image
   return Json ( new { Status="success", Message="Uploaded successfully" });
}

並且在done()事件中,您可以檢查json響應並根據需要顯示錯誤消息。

$('#fileupload').fileupload({
    url: '/SmartphonePhotographer/ManageFiles?ResponseID=' + ResponseID,
    error: function (e, data,txt) {
        alert('error' + txt);
    }
}).done(function(response){
   if(response.Status==="error")
   {
       $.each(services.Errors, function (a, b) {
         alert(b);
       });
   }       
});

使用這種方法,您可以將多個驗證錯誤發送回客戶端,客戶端可以對其進行處理(顯示給用戶?)。

MVC 6

在MVC6中,您可以直接從MVC控制器操作返回HttpStatusCode響應。 因此,您無需自己發送JSON響應。

 [HttpPost]
 public IActionResult SaveImage()
 {
     var customErrors = new List<string> { "File format is not good", 
                                            "File size is too bib" };

     return HttpBadRequest(customErrors);
 }

這會將400響應與我們在響應中傳遞的數據(錯誤列表)一起發送給調用者。 因此,您可以訪問error事件的錯誤xHr對象的responseJSON屬性以獲取它

error: function (a, b, c) {           
     $.each(a.responseJSON, function (a, b) {
            alert(b);
     });
 }

我同意您的問題是,您拋出異常而不是返回受控響應。 大多數框架會尋找400x或500x的狀態碼。 因此,您想返回一個友好的json對象和這些范圍內的狀態代碼。 如果這樣做,您在錯誤塊中的數據對象將是您返回的內容。

MVC土地:

//get a reference to request and use the below.
return this.Request.CreateResponse(HttpStatusCode.BadRequest, "Your message here");

網絡API 2

使用IHttpActionResult並返回BadRequest("My error message"); 如果這樣做,它將設置您的狀態碼並返回響應作為數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM