[英]MVC FileContentResult for Image returns binary to screen
这是一个头擦。 我有在一个项目中运行良好的代码。 我在另一个地方重用它,但是它不起作用。 不知道我错过了什么。 我正在尝试将图像从我的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"
当图像返回时,图像控件将写出二进制文件而不是图像。 就像我说的那样,这在另一个项目中效果很好。 我忘记了什么吗?
原来,我正在跳过请求包含我的图像控件的局部视图的直接中间步骤,而直接请求图像二进制文件,这正是我所得到的。 所以我改变了:
$.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包含图像控件,该图像控件随后请求二进制文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.