ImageResizer is not resizing images served by WebA

2019-08-27 22:20发布

I am trying to get ImageResizer to resize images which are served by WebAPI. I have ImageResizer installed as in documentation. /resizer.debug.ashx shows no issues. When I try to resize static image, it works fine.

I tried resizing with these parameters:

    /api/files/image/image.jpg?width=100
    /api/files/image/image.jpg?width=100&process=always
    /api/files/image/image.jpg?width=100&format=jpg

My WebAPI action looks like this:

    [HttpGet]
    public HttpResponseMessage Image(string name)
    {
        var filePath = GetFilePath();

        var response = new HttpResponseMessage(HttpStatusCode.OK);
        response.Content = new StreamContent(new FileStream(filePath, FileMode.Open, FileAccess.Read));
        response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("inline");
        response.Content.Headers.ContentDisposition.FileName = fileData.OriginalFileName;
        response.Content.Headers.ContentLength = fileData.ContentLength;
        response.Content.Headers.ContentType = new MediaTypeHeaderValue(fileData.MediaType);

        return response;
    }

Is there a way to use ImageResizer with WebAPI?

1条回答
唯我独甜
2楼-- · 2019-08-27 22:33

WebAPI, MVC, and HTTPHandlers all have one thing in common - terrible file serving performance. Application frameworks, by design, aren't tuned for large file handling.

Also, due to ASP.NET and IIS pipeline limitations, you can't efficiently cache and serve images when the file is being produced by an HttpHandler/MVC/WebAPI during the ExecuteRequest phase.

Please read the ImageResizer Best Practices Guide for more information.

If you need to provide source images, you can implement IVirtualImageProvider or use one of the provided plugins to access your data store of choice.

查看更多
登录 后发表回答