Ver*_*kis 6 c# asp.net anchor httphandler
我在我的项目中有一个页面DownloadDocument.aspx
,它的代码隐藏是DownloadDocument.aspx.cs
在我的DownloadDocument.aspx
我有一个锚点,采取这样的动态链接:
<a id="downloadLink" runat="server" style="margin:5px"
href="<%# CONTENT_DIRECTORY_ROOT + document.Path %>">Download current file</a>
Run Code Online (Sandbox Code Playgroud)
我想添加一个httphandler来控制下载的文件名,我该怎么做?提前致谢.
cit*_*nas 16
如何使用通用处理程序(.ashx)?
您需要添加特定于加载的信息,例如filename,contenttyp和内容本身.样本应该给你一个良好的开端.
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
通用处理程序是从URL调用的,如下所示:
<a href="/GetDownload.ashx?IDDownload=1337">click here to download</a>
Run Code Online (Sandbox Code Playgroud)