简体   繁体   English

MVC FileContentResult for Image将二进制返回到屏幕

[英]MVC FileContentResult for Image returns binary to screen

This is a head scratcher. 这是一个头擦。 I have code that works great in one project. 我有在一个项目中运行良好的代码。 I am reusing it in another and it doesn't work. 我在另一个地方重用它,但是它不起作用。 Not sure what I missed. 不知道我错过了什么。 I am trying to return an image from my MVC controller to an image control. 我正在尝试将图像从我的MVC控制器返回到图像控件。 Method is: 方法是:

public ActionResult ScannedImage(ImageSideIndicator side)
    {
        try
        {
            ScannedItemViewModel model = (ScannedItemViewModel)Session["selectedItem"];

            String imagePath = side.ImageSide == 1 ? model.Item.FrontImagePath : model.Item.RearImagePath;
            byte[] image = CA.ImageStreamer.Image.FromFile(imagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
            if (image == null)
            {
                return File(ImageNotFound(), "image/png");
            }
            else if (image.Length == 0)
            {
                return File(ImageNotFound(), "image/png");
            }

            model = null;
            return File(image, "image/jpeg");
        }
        catch (Exception ex)
        {                
            return File(ImageNotFound(), "image/png");
        }
    }

And the image control is: 图像控件为:

img id="imgCheckImage" class="imageBorder" style="max-height:100%;max-width:100%" alt="Check Image" src="Scan/ScannedImage?ImageSide=@Model.ImageSide&cachePreventer=@DateTime.Now.Millisecond" 

When the image comes back the image control writes out the binary instead of the image. 当图像返回时,图像控件将写出二进制文件而不是图像。 Like I said this works fine in another project. 就像我说的那样,这在另一个项目中效果很好。 Did I forget something? 我忘记了什么吗?

Turns out I was skipping the intermediate step of requesting the partial view that contained my image control and was directly requesting the image binary, and that's exactly what I got. 原来,我正在跳过请求包含我的图像控件的局部视图的直接中间步骤,而直接请求图像二进制文件,这正是我所得到的。 So i changed: 所以我改变了:

 $.ajax({
        url: '@Url.Action("ScannedImage", "Scan")',
        type: 'POST',
        dataType: 'html',
        cache: false,
        data: { ImageSide: currentSide },
        success: function (data, status, xhr) {
            if (SessionTimedOut(data)) {
                RedirectToLogin();
                return;
            }
            $('#divPanZoomImage').html(data);
        },
        error: function (xhr, status, error) {
            alert(error);
        }
    });

To: 至:

 $.ajax({
        url: '@Url.Action("ShowScannedImage", "Scan")',
        type: 'POST',
        dataType: 'html',
        cache: false,
        data: { ImageSide: currentSide },
        success: function (data, status, xhr) {
            if (SessionTimedOut(data)) {
                RedirectToLogin();
                return;
            }
            $('#divPanZoomImage').html(data);
        },
        error: function (xhr, status, error) {
            alert(error);
        }
    });

Where ShowScannedImage returned a PartialViewResult that contained the image control which then requests the binary. ShowScannedImage返回的PartialViewResult包含图像控件,该图像控件随后请求二进制文件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM