asp.net ashx handler prompting download instead of displaying file

后端 未结 3 1177
逝去的感伤
逝去的感伤 2021-01-20 22:11

I implemented a generic handler in my application which works great for images, but when I manually type the handler URL in the browser with the image\'s querystring it prom

相关标签:
3条回答
  • 2021-01-20 22:46

    If you store the ContentType as part of the files metadata, when you pull it back down your could use it.

    theFile = GetFile(id)
    context.Response.ContentType = theFile.Type;
    
    0 讨论(0)
  • 2021-01-20 23:00

    The only way is to specify correct ContentType so the browser know what to do with receiving file, depending on installed plugins (for example, view pdf files in browser frame) and system assosiations (for example, offer to open document in MS Office instead of simple download)

    You can try to specify Content Type depending on file extension, i.e.:

    if(Path.GetExtension(path) == ".jpg")
       context.Response.ContentType = "image/jpeg";
    else
       context.Response.ContentType = "application/octet-stream";
    
    0 讨论(0)
  • 2021-01-20 23:02

    The content-disposition header is the one that causes your browser to show the download dialog. Remove that line and it will show in the browser.

    0 讨论(0)
提交回复
热议问题