2017-02-01 228 views
2

以base64格式返回圖像,我有以下的代碼來裁剪圖像:無法加載資源:服務器500(內部服務器錯誤)的狀態迴應,同時從控制器

$.ajax({ 
     type: "POST", 
     url: root("CropController/CropImage"), 
     data: { 
      imagePath: imagesrc, 
      cropPointX: parseInt(PointX), 
      cropPointY: parseInt(PointY), 
      imageCropWidth: parseInt(CropWidth), 
      imageCropHeight: parseInt(CropHeight) 
     } 
    }).done(function (dt) { 
     alert(dt.photo); 
    }); 

而控制器代碼是:

public JsonResult CropImage(string imagePath, int? cropPointX, int? cropPointY, int? imageCropWidth, int? imageCropHeight) 
    { 
     string output = imagePath.Substring(imagePath.IndexOf(',') + 1); 
     byte[] imageBytes = Convert.FromBase64String(output); 

      //croppedImage will have the cropped part of the image. 
     byte[] croppedImage = ImageCroping.CropImage(imageBytes, cropPointX.Value, cropPointY.Value, imageCropWidth.Value, imageCropHeight.Value); 

     string photo = "data:image/jpeg;base64," + Convert.ToBase64String(croppedImage); 
     return Json(new { photoPath = photo }, JsonRequestBehavior.AllowGet); 

    } 

如果種植面積較小,則在完成功能警報將被調用,但是當種植面積大,做功能不會觸發它拋出的錯誤。任何人都可以幫助我這個。在此先感謝

+0

你見過[這](http://stackoverflow.com/a/12278956/1429080 )回答? – user1429080

回答

1

嘗試使用ContentResult而不是JSON。這似乎JSON有問題,它的長度和它的一些錯誤的字符,也可以與它的GET允許的回報:

public ActionResult CropImage(string imagePath, int? cropPointX, int? cropPointY, int? imageCropWidth, int? imageCropHeight) 
    { 
     string output = imagePath.Substring(imagePath.IndexOf(',') + 1); 
     byte[] imageBytes = Convert.FromBase64String(output); 

      //croppedImage will have the cropped part of the image. 
     byte[] croppedImage = ImageCroping.CropImage(imageBytes, cropPointX.Value, cropPointY.Value, imageCropWidth.Value, imageCropHeight.Value); 

     string photo = "data:image/jpeg;base64," + Convert.ToBase64String(croppedImage); 
     return Content(photo); 

    } 
+0

ContentResult顯示錯誤。 – Shareef

+0

@Shareef什麼是錯誤? –

+0

錯誤是ContentResult是一個類型,但像變量一樣使用。我不確定錯誤,但通過返回如下解決它: public stringCropImage(string imagePath,int?cropPointX,int?cropPointY,int?imageCropWidth,int?imageCropHeight){ .......... ........ 返回照片; } – Shareef

相關問題