2017-03-09 22 views
3

基本上,我的觀點是試圖同時顯示縮略圖和全尺寸的圖像:力控制器行動回報「類型」的圖片,而沒有以文件

<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(縮略圖)布爾值。

  • 當我通過真實的,瀏覽器的http請求被解釋爲類型:jpeg
  • 當我通過假的,它被解釋爲document

請檢查以下PRINTSCREEN:

enter image description here

我該如何強制控制器動作返回類型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。是的,如果我在縮略圖旁邊顯示完整圖像,那麼它們都會被處理爲「圖像」....但是您可以看到我添加「a」html標籤的方式使用相同的路徑。但是,現在看來lightcase.js以某種方式將它處理爲文檔而不是圖像,可能通過嘗試讀取「路徑擴展名」並將其與圖像匹配或不匹配......我真的認爲這是現在的問題。

<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:[email protected](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

+0

你說的「文件」的意思是,在什麼確切的內容類型做您的瀏覽器報告答案是? – christofr

+0

@christofr嗨,我在問題描述中包含了一個打印屏幕。 – Dryadwoods

+0

我所看到的不僅是'類型',還有'發起者'。如果手動輸入這兩個URL並檢查它們將返回什麼(根本不使用JS)會怎麼樣? (也許這將有助於減少搜索區域)。 – MaKCbIMKo

回答

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 ...

這不是最終的解決方案,但至少可以證明問題出在哪裏。

好吧,從這裏我知道我能做什麼以及如何解決這個問題。

2

我注意到了關於lightcase.js的兩件事:
- 他們總是使用鏈接而不是圖片。即使圖像被顯示,它也在<a>元素內。因此,請嘗試使用<a>包裝您的<img />
- 他們檢查網址以某些預定義的擴展名結尾。從行114(typeMapping)和行752(_verifyDataType函數)檢查source。如果添加虛擬查詢字符串參數,可能會有所幫助,例如: Images/GetImage/60?s=1&t=True&type=.jpg。你會忽略它的服務器端,但可能有助於燈箱

我現在不能嘗試它,但我希望它可以幫助。

1

在過去與的ActionResult返回文件時我有奇怪的問題專門文件編碼,請返回FileContentResult

+0

Obrigado pela resposta,mas no meu caso nada mudou:P Forca ai na Irlanda。 – Dryadwoods

相關問題