简体   繁体   中英

ImageResizer.net and Owin UseStaticFiles

Im using ImageResizer and using Owin StaticFiles like:

app.UseStaticFiles(new StaticFileOptions
        {
            RequestPath = new PathString("/photos/user"),
            FileSystem = new PhysicalFileSystem(@".\uploads\photos"),
           // EnableDirectoryBrowsing = true,
        });

web.config

<system.webServer>
    <handlers>
      ... 

        <add name="Owin" verb="" path="*" type="Microsoft.Owin.Host.SystemWeb.OwinHttpHandler, Microsoft.Owin.Host.SystemWeb" />   
      ....
</handlers>

ImageResizing is well configured i can confirm that by commenting the Owin handler in webconfig

However when i use both together i seem unable to use ImageResizer

like

http://localhost:7805/photos/user/admin.png?w=120

Image is not resized to specified width

I just had the same problem and found a solution that works for me. If it's too late for you, maybe it helps someone else.

I created the following middleware for my images:

app.Use(async (ctx, next) =>
{
  var endings = new[] { ".jpg", ".jpeg", ".png", ".gif" };
  if (endings.Any(e => ctx.Request.Path.Value.EndsWith(e)) &&
      ctx.Request.QueryString.HasValue &&
      File.Exists(HostingEnvironment.MapPath(ctx.Request.Path.Value)))
  {
    var inputStream = File.OpenRead(HostingEnvironment.MapPath(ctx.Request.Path.Value));

    var outputStream = ctx.Response.Body;

    var job = new ImageJob(inputStream, outputStream, new Instructions(ctx.Request.QueryString.Value));

    var cfg = new ImageResizer.Configuration.Config();
      cfg.Build(job);
  }
  else
  {
      await next();
  }
});

I put it before the "UseStaticFiles" in my Startup. Of course you can change your configuration to your likes/needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM