简体   繁体   English

加载资源失败:服务器响应状态为 500(内部服务器错误),同时从控制器返回 base64 格式的图像

[英]Failed to load resource: the server responded with a status of 500 (Internal Server Error) while returning image in base64 format from controller

I have the following code to crop the image :我有以下代码来裁剪图像:

   $.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);
    });

And the controller code is :控制器代码是:

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);

    }

If the cropping area is small then the alert in done function will be called but it throws the error when the cropping area is large and the done function is not fired.如果裁剪区域很小,则将调用 done 函数中的警报,但是当裁剪区域很大且未触发 done 函数时,它会抛出错误。 Can anyone please help me with this.任何人都可以帮我解决这个问题。 Thanks in advance提前致谢

Try using ContentResult instead of JSON.尝试使用 ContentResult 而不是 JSON。 It seems json has problem with its length or some wrong character in it Or may be with its GET allowed on return :似乎 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);

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 加载资源失败:服务器响应状态为500(内部服务器错误 - Failed to load resource: the server responded with a status of 500 (Internal Server Error 无法加载资源:httpPost时,服务器的状态为500(内部服务器错误) - Failed to load resource: the server responded with a status of 500 (Internal Server Error) while httpPost Ajax错误:无法加载资源:服务器响应状态为500(内部服务器错误) - Ajax error : Failed to load resource: the server responded with a status of 500 (Internal Server Error) 加载资源失败:服务器响应状态为500(内部服务器错误)Ajax Rails错误 - Failed to load resource: the server responded with a status of 500 (Internal Server Error) Ajax Rails error 无法加载资源:服务器响应的状态为403(禁止)和500(内部服务器错误) - Failed to load resource: the server responded with a status of 403 (Forbidden) and 500 (Internal Server Error) 无法加载资源:服务器响应状态为 500(内部服务器错误)-NodeJs Express & MongoDB Web 应用程序 - Failed to load resource: the server responded with a status of 500 (Internal Server Error) - NodeJs Express & MongoDB Web Application 加载资源失败:服务器响应状态为500(内部服务器错误)mvc - Failed to load resource: the server responded with a status of 500 (Internal Server Error) mvc 无法加载资源:服务器响应状态为500(内部服务器错误)-无法发布数据表单 - Failed to load resource: the server responded with a status of 500 (Internal Server Error) - CANNOT POST DATA FORM 无法加载资源:服务器以MVC ASP.NET的状态响应500(内部服务器错误) - Failed to load resource: the server responded with a status of 500 (internal server error) for MVC ASP.NET 加载资源失败:服务器响应状态为500(内部服务器错误)MVC JAVASCRIPT - Failed to load resource: the server responded with a status of 500 (Internal Server Error) MVC JAVASCRIPT
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM