0

这是一个令人头疼的问题。我的代码在一个项目中效果很好。我在另一个中重用它,但它不起作用。不知道我错过了什么。我正在尝试将图像从我的 MVC 控制器返回到图像控件。方法是:

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

图像控制是:

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" 

当图像返回时,图像控件会写出二进制而不是图像。就像我说的那样,这在另一个项目中效果很好。我是不是忘记了什么?

4

1 回答 1

0

结果我跳过了请求包含我的图像控件并直接请求图像二进制文件的部分视图的中间步骤,这正是我得到的。所以我改变了:

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

至:

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

ShowScannedImage 返回一个包含图像控件的 PartialViewResult,然后请求二进制文件。

于 2016-04-20T16:08:46.120 回答