我有以下代码来裁剪图像:
$.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);
});
Controller 代码是:
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);
}
如果裁剪区域很小,那么将调用done函数中的警报,但当裁剪区域很大并且未触发done函数时,它会抛出错误。谁能帮我解决这个问题吗?提前致谢
最佳答案
尝试使用 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);
}
关于javascript - 无法加载资源: the server responded with a status of 500 (Internal Server Error) while returning image in base64 format from controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41975833/