2014-11-08 53 views
1

我想要從我的站點下載附件的能力,(我需要它運行在IE) 所以在HTML我有:爲什麼在ASP.NET MVC中我得到JSON而不是FileDownload?

 [HttpGet] 
     public FileContentResult DownloaAttachment(int id) 
     { 
      var att = GetAttachmentById(id); 
      byte[] fileBytes = att.Content.Content; 
      var response = new FileContentResult(fileBytes, MediaTypeNames.Application.Octet); 
      response.FileDownloadName = att.FileName; 
      return response; 
     } 

<a href="api/attachments/DownloaAttachment?id={{attachment.Id}}" target="_blank"> 
    Download Image 
</a> 
<!-- {{attachment.Id}} - it's because I use AngularJS :) --> 
在控制器

但是當我點擊「下載圖像」 - 我有一個新窗口,並從控制器作爲json響應,如:

{"FileContents":"/9j/4.. some bytes..ZK9k=","ContentType":"application/octet-stream","FileDownloadName":"Cover.jpg"} 

但我不需要那個JSON,我需要能夠將附件作爲文件下載到用戶的計算機上。 我怎樣才能使它成爲簡單的文件下載?我做錯了什麼?

回答

0

嘗試修改此行:

var response = new FileContentResult(fileBytes, MediaTypeNames.Application.Octet); 

到:

var response = new FileContentResult(fileBytes, "image/jpeg"); 
+0

我試着改變你寫的,但沒有什麼變化。 - 打開新窗口,我看到{「FileContents」:「/ 9j/4AAQSkZJRg ... bla..bla..Afbs // Z」,「ContentType」:「image/jpeg」,「FileDownloadName」:「CoverBack.JPG 「}正確的在一個頁面上.. – paulpeters 2014-11-09 12:15:00

0

希望這個工程。

[HttpGet] 
    public HttpResponseMessage DownloaAttachment(int id) 
    { 
     var att = GetAttachmentById(id); 
     byte[] fileBytes = att.Content.Content; 
     var response = new FileContentResult(fileBytes, MediaTypeNames.Application.Octet); 
     response.FileDownloadName = att.FileName; 
     return Request.CreateResponse(HttpStatusCode.OK, response, new MediaTypeHeaderValue("image/*")); 

    } 

或者如果你有文件擴展名而不是*在(image/*)。

指的是:MSDN

相關問題