Question

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="" />
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top