![](/img/trans.png)
[英]Download file of any type in Asp.Net MVC using FileResult?
[英]Using ImageResizer with FileResult, ASP.NET MVC?
我們有以下用於獲取文件的控制器:
[SessionState(SessionStateBehavior.Disabled)]
public class FileController : BaseController
{
[HttpGet]
public FileResult Index(long id)
{
if (id <= 0) return null;
Attachment attachment = Service.GetAttachmentById(id);
if (attachment == null) return null;
new Task(() => Service.IncreaseAttachmentViewCount(id)).Start();
string filename = attachment.Name;
//"application/octet-stream";
string mimeType = System.Web.MimeMapping.GetMimeMapping(filename);
return File(attachment.Path, mimeType);
}
}
可以,效果很好,但不能與ImageResizer API一起使用
例如:
下圖以自然尺寸(1920 * 1200)顯示,並且maxwidth或maxheight根本不起作用。 但是,如果我使用絕對文件路徑,它將起作用。
<img src="File/Index/1231?maxwidth=300&maxheight=300" alt="" />
通過任何類型的應用程序框架(無論MVC,WebAPI,Rails,WebForms還是HttpHandlers)提供大型二進制文件通常是一個壞主意。
《 ImageResizer最佳做法》指南對此進行了詳細說明。
為了獲得良好的性能,您應該將其實現為URL重寫(或IVirtualImageProvider)。
處理ImageResizer的Config.Current.Pipeline.Rewrite
事件,然后自己解析路徑。 根據您解析的ID修改路徑,並增加視圖計數器。 這應該需要5-8行代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.