2017-02-01 176 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

相关问题