我的项目中有一个页面DownloadDocument.aspx
它的代码隐藏是DownloadDocument.aspx.cs
In my DownloadDocument.aspx
我有一个锚点,它采用如下动态链接:
<a id="downloadLink" runat="server" style="margin:5px"
href="<%# CONTENT_DIRECTORY_ROOT + document.Path %>">Download current file</a>
我想添加一个httphandler来控制下载的文件名,我该怎么做?提前致谢。
为此使用通用处理程序 (.ashx) 怎么样?
您需要添加加载特定信息,例如文件名、内容类型和内容本身。该示例应该会给您一个良好的开端。
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;
}
}
}
通用处理程序是从 URL 调用的,如下所示:
<a href="/GetDownload.ashx?IDDownload=1337">click here to download</a>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)