问题描述
我们有获取文件以下控制器:
[SessionState会(SessionStateBehavior.Disabled)
公共类FileController:BaseController
{
[HTTPGET]
公共FileResult指数(长ID)
{
如果(ID< = 0)返回NULL;
附件附件= Service.GetAttachmentById(ID);
如果(附件== NULL)返回NULL;
新的任务(()=> Service.IncreaseAttachmentViewCount(ID))。开始();
字符串文件名= attachment.Name;
//应用程序/八位字节流;
串mime类型= System.Web.MimeMapping.GetMimeMapping(文件名);
返回文件(attachment.Path,mime类型);
}
}
这是确定的,并且效果很好,但它不与 API
例如:
下图显示了在自然尺寸(1920 * 1200),并了maxWidth或了maxHeight根本不工作。但是,如果使用绝对文件路径它的工作原理。
< IMG SRC =文件/索引/ 1231 =了maxWidth 300安培;了maxHeight = 300ALT =/>
通过任何类型的应用程序框架提供大型的二进制文件(无论是MVC,的WebAPI,Rails的,的WebForms或HttpHandlers的)通常是一个坏主意。
借助更详细地解释。
要获得良好的性能,你应该实现这个作为URL重写(或IVirtualImageProvider)来代替。
处理ImageResizer的 Config.Current.Pipeline.Rewrite
事件,并分析自己的路径。修改根据您解析的ID路径,并增加你的看法计数器。这应该需要5-8线code的。
We have the following Controller for getting file:
[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);
}
}
It's OK and works well but it doesn't work with ImageResizer API
e.g:
The following image shows in natural size(1920*1200) and maxwidth or maxheight doesn't work at all. But if I use the absolute file path it works.
<img src="File/Index/1231?maxwidth=300&maxheight=300" alt="" />
Serving large binary files via an application framework of any kind (whether MVC, WebAPI, Rails, WebForms, or HttpHandlers) is generally a bad idea.
The ImageResizer Best Practices guide explains in more detail.
To get good performance, you should implement this as a URL rewrite (or IVirtualImageProvider) instead.
Handle ImageResizer's Config.Current.Pipeline.Rewrite
event, and parse the path yourself. Modify the Path based on the ID you parsed, and increment your view counter. This should require 5-8 lines of code.
这篇关于使用ImageResizer与FileResult,ASP.NET MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!