Using ImageResizer with FileResult, ASP.NET MVC?

2019-09-01 01:26发布

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="" />

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-09-01 01:49

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.

查看更多
登录 后发表回答