强制控制器动作返回"键入"作为图像而不是文档

时间:2017-03-09 12:37:50

标签: c# asp.net-mvc lightcase.js

基本上,我的观点是尝试同时显示缩略图和全尺寸图像:

<img src="/Images/GetImage/60?s=1&t=True" class="" style="width: 100px; height: 80px;" data-id="60" data-source="1">

<img src="/Images/GetImage/60?s=1&t=False" class="" style="width: 100px; height: 80px;" data-id="60" data-source="1">

唯一改变的是t(缩略图)布尔值。

  • 当我传递true时,浏览器http请求被解释为Type:jpeg
  • 当我传递false时,它被解释为document

请检查以下打印屏幕:

enter image description here

我如何才能真正强制Controller Action将输入作为Image而不是document

我的实际源代码:

public async Task<ActionResult> GetImage(int id, EnumImageSource s, bool t)
{
    var backupImagePath = Server.MapPath(Url.Content("~/Content/Images/PhotoNotAvailable.png"));
    var originalImagePath = Server.MapPath(Url.Content("~/Content/Images/PhotoNotAvailable.png"));
    var finalImagePath = Server.MapPath(Url.Content("~/Content/Images/PhotoNotAvailable.png"));            

    ...
    ...
    ...
    ...

    if (model != null)
    {
       ...
       ...
       ...
        originalImagePath = model.Object.Path;                
    }

    if (t)
    {
        var extension = System.IO.Path.GetExtension(originalImagePath);
        var thumbImagePath = System.IO.Path.ChangeExtension(originalImagePath, null) + "_thumb" + extension;
        if (System.IO.File.Exists(thumbImagePath))
        {
            finalImagePath = thumbImagePath;
        }
        else
        {
            if (System.IO.File.Exists(originalImagePath))
            {
                int width;
                int height;
                if (ProcessImage.GetDimentionsByImageType(EnumImageSize.Thumbnail, out width, out height))
                {
                    try
                    {
                        if (ProcessImage.ResizeImageFile(width, height, originalImagePath, thumbImagePath))
                        {
                            finalImagePath = thumbImagePath;
                        }
                    }
                    catch
                    {

                    }
                }
            }                    
        }   
    }
    else
    {
        if (System.IO.File.Exists(originalImagePath))
        {
            finalImagePath = originalImagePath;
        }
    }
    var bytes = System.IO.File.ReadAllBytes(finalImagePath);
    Response.ContentType = "image/jpeg";
    return File(bytes, "image/jpeg");
}

更新#1

按照MaKCbIMKo的要求。是的,如果我在缩略图旁边显示完整的图像,那么它们都被处理为&#34;图像&#34; ....但是你可以看到我添加&#34; a&#34的方式; html标签使用相同的路径...但是,现在看来lightcase.js以某种方式将其处理为文档而不是图像,可能是通过尝试读取&#34;路径扩展&#34;并与图像匹配与否......我现在认为这是问题。

<div class="img-wrap">
        @if (Model.IsReadOnly == false)
        {
            <span class="close">&times;</span>
        }
        <a href="@(Url.Action("GetImage", "Images", new { area = "", id = i.Id, s = (int)i.ImageSource, t = false }))" data-rel="lightcase:myCollection@(Model.SourceId)" class="showcase">
            <img src="@(Url.Action("GetImage", "Images", new {area = "", id = i.Id, s = (int) i.ImageSource, t = true}))" class="" style="width: 100px; height: 80px;" data-id="@i.Id" data-source="@((int)Model.ImageSource)" />
        </a>
        FULL IMAGE
        <img src="@(Url.Action("GetImage", "Images", new {area = "", id = i.Id, s = (int) i.ImageSource, t = false}))" class="" style="width: 100px; height: 80px;" data-id="@i.Id" data-source="@((int)Model.ImageSource)" />
    </div>

enter image description here

3 个答案:

答案 0 :(得分:2)

似乎问题是lightcase.js代码....

我刚测试了源代码的快速更改:

createObject: function () {
        var $object;

        // Create object
        switch (_self.objectData.type) {
            ....
         default:

                $object = $(new Image());
                $object.attr({
                    // The time expression is required to prevent the binding of an image load
                    'src': _self.objectData.url,
                    'alt': _self.objectData.title
                });
                break;

现在我将图像显示为图像而不是iframe ....

这不是最终解决方案,但至少可以证明问题所在。

好的,从这里我知道我能做什么以及如何解决这个问题。

答案 1 :(得分:2)

我注意到有关lightcase.js的两件事:
- 他们总是使用链接而不是图像。即使显示图像,它也在<a>元素内。因此,请尝试用<img />包裹<a> - 他们检查网址是否以某些预定义的扩展名结尾。从第114行(typeMapping)和第752行(_verifyDataType函数)检查source。如果你添加虚拟查询字符串参数,它可能会有所帮助: Images/GetImage/60?s=1&t=True&type=.jpg。您将忽略它的服务器端,但可能有助于使用lightcase

我现在不能尝试,但我希望它有所帮助。

答案 2 :(得分:1)

过去我在使用 ActionResult 特别是文件编码返回文件时遇到了一些奇怪的问题尝试返回 FileContentResult