ASP.NET add a httphandler to edit downloaded file name

梦想与她 提交于 2019-12-01 04:30:43

How about using a generic handler (.ashx) for this?

You need to add loading specific information, like filename, contenttyp and the content itself. The sample should give you a good headstart.

public class GetDownload : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        if (!string.IsNullOrEmpty(context.Request.QueryString["IDDownload"]))
        {
                context.Response.AddHeader("content-disposition", "attachment; filename=mydownload.zip");
                context.Response.ContentType = "application/octet-stream";
                byte[] rawBytes = // Insert loading file with IDDownload to byte array
                context.Response.OutputStream.Write(rawBytes, 0, rawBytes.Length);
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

The generic handler is called from a URL, like this:

<a href="/GetDownload.ashx?IDDownload=1337">click here to download</a>

it depends on type of file you are trying to download...because every request is gone through HTTPHandler's ProcessRequest. and it's checks each and every request one by one.. You need to add any HTTPHandler to your project and need to add something like this in your web.config.

 <httpHandlers>
  <add path="*.jpg,*.jpeg,*.bmp,*.tif,*.tiff" verb="*" type="NameofYourHandler" />
</httpHandlers>

This will check your request for every Image type.. mentioned in path attribute

Edit :

<add verb="*" path="*DownloadDocument.aspx " type="NameofYourHandler"/>

You can try with this code

<httpHandlers>
  <add 
   verb="POST"  
   path="*.jpg,*.jpeg,*.bmp,*.tif,*.tiff" 
   type="YourHandler" />
</httpHandlers>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!