ASP.NET add a httphandler to edit downloaded file name

前端 未结 3 1682
野趣味
野趣味 2021-01-12 20:46

I have in my project a page DownloadDocument.aspx and it\'s codebhind is DownloadDocument.aspx.cs

In my DownloadDocument.aspx

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-12 21:46

    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:

    click here to download
    

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题