javascript - 无法加载资源: the server responded with a status of 500 (Internal Server Error) while returning image in base64 format from controller

标签 javascript c# jquery ajax asp.net-mvc

我有以下代码来裁剪图像:

   $.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/

相关文章:

javascript - 使用 jQuery 在自定义 UL LI 框中触发更改事件

c# - 命名空间 'routing' visual studio 中不存在类型或命名空间名称 'system.web'

c# - 什么时候使用公共(public)字段才有意义?

jquery - CKEditor 更新 texarea 值时出现问题

javascript - angular组件测试如何使用外部js库

javascript - 有没有办法在浏览器中访问蓝牙连接的设备?

javascript - 三星智能电视叠加视频

c# - 如何在遍历 Treeview 时设置位置

javascript - 如何让 jquery/javascript 函数触发一次

javascript - 使用 Bootstrap 进行表单验证 (jQuery)