HttpHandler下载txt文件(ASP.NET)?
嘿,我创建了一个 HttpHandler 用于从服务器下载文件。看起来它没有处理任何东西......我在 ProcessRequest 中放置了一个断点,它永远不会去那里。
public class DownloadHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//download stuff and break point
}
}
正如前面提到的,它永远不会止步于此。我也在 web.config 中注册了它。
<add verb="*" path="????" type="DownloadHandler" />
我不确定该条目的路径部分。我必须在那里输入什么?我正在下载 txt 文件,但 URL 不包含文件名,我必须以某种方式将其传递给处理程序。我该怎么做?也许是会话?
谢谢
Hey, I created a HttpHandler for downloading files from the server. It seems it is not handling anything...I put a breakpoint in the ProcessRequest, it never goes there.
public class DownloadHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//download stuff and break point
}
}
It never stops there, as mentioned. I also registered it in the web.config.
<add verb="*" path="????" type="DownloadHandler" />
I am not sure about the path part of that entry. What do I have to enter there? I am downloading txt files, but the URL does not contain the filename, I somehow have to pass it to the handler. How would I do this? Session maybe?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否阅读过如何注册 Http 处理程序?您使用的是 IIS 6 还是 7?
路径部分应包含一个(部分)url,因此如果您使用的是不带文件名的静态 url,则应将其放在那里。您可以以不存在的资源名称结束 url,并将其映射到路径,
例如 url 为 http:// /myserver.com/pages/downloadfiles
并且 path="downloadfiles"
如果执行 POST,则可以将文件名放在隐藏字段中,然后将其提取到处理程序中。如果您使用 GET,我不确定,要么交叉发布视图状态,要么像您所说的那样将文件名放入会话中。
为什么不能将文件名放入 url 中?
Have you read How to register Http Handlers? Are you using IIS 6 or 7?
The path part should contain a (partial) url, so if in your case you are using a static url without the filenames, you should put that there. You can end the url in the name of a non-existent resource and map that to path
e.g. the url is http://myserver.com/pages/downloadfiles
and the path="downloadfiles"
If you do POST, you can put the filename in a hidden field, and extract it in the handler. If you're using GET, I'm not sure, either cross-post the viewstate or put the filename in the session like you said.
Any reason why you can't put the filename in the url?
处理程序的路径必须是您尝试处理的路径 - 我知道这有点同义反复,但就是这么简单。无论您网站上的路径是什么(真实的或更可能是虚拟的),您都希望由该处理程序处理。
现在,除非该路径末尾的文件类型通常由 ASP.NET 处理(例如 .aspx、.asmx,但不是 .txt),ASP 将永远不会看到该请求,以便它能够穿过它的管道并最终到达您的处理程序。在这种情况下,您必须将 IIS 中的扩展类型绑定到 ASP.NET。
至于确定处理程序应该响应什么文件,您可以通过多种方式实现这一点 - 我强烈建议避免会话或 cookie 或任何临时和隐式的内容。相反,我建议使用查询字符串或表单值,基本上任何将显示为请求标头的内容。
Fianlly,我必须问为什么你要为此使用处理程序 - .txt 通常可以正常使用,那么你想在这里实现什么附加功能?很可能有更好的方法。
The path for a handler needs to be the path you are trying to handle - bit of a tautology I know but it's as simple as that. Whatever path on your site (real or much more likely virtual) you want to be handled by this handler.
Now unless the kind of file at the end of that path is normally handled by ASP.NET (e.g. .aspx, .asmx but not a .txt) ASP will never see the request in order for it to go through it's pipeline and end up at your handler. In that case you have to bind the extension type in IIS to ASP.NET.
As far as identifying what file the handler is supposed to respond with you could achieve this any number of ways - I would strongly recommend avoiding session or cookies or anything temporal and implicit. I would instead suggest using the querystring or form values, basically anything which will show up as a request header.
Fianlly, I have to ask why you're using a handler for this at all - .txt will serve just fine normally so what additional feature are you trying to implement here? There might well be a better way.