我们有一个自定义的IHttpHandler,负责文件下载。该处理程序被映射到URL /downloadfile.rem。
The browser redirects user to URL formatted like:
/downloadfile.rem/[fileID]/[mimeType]/[fileName].[extension]
example:
/downloadfile.rem/54923876129874545456621/text$plain/data.txt
我们的处理程序从URL获取提供的信息,并响应文件数据。
这种方法适用于IIS 6和IIS 7,但我们在ASP.NET开发服务器上遇到了问题。似乎,IIS 6和IIS 7从左到右查看URL并在"downloadfile.rem"部分停止,并正确调用我们的自定义处理程序。ASP.NET开发服务器似乎从右到左查看URL,并根据URL末尾的扩展名决定如何处理文件。这会导致404响应。当我们从URL末尾删除扩展名时,一切都按预期工作。
这是我们在httpHandlers部分的条目:
<添加动词="GET" 路径="downloadfile.rem" 类型="OurNamespace.DownloadFileHandler"/>
这是我们在处理程序部分的参赛作品:
<添加 name="DownloadFile" verb="GET" path="downloadfile.rem" type="OurNamespace.DownloadFileHandler"/>
如何在ASP.NET开发服务器中正确运行?
Rationale - why we do this the way we do it?
The files for download are generated dynamically as a result of an Ajax request. Since it is an Ajax request, we cannot return the file directly to the browser, but store it on the disk for the browser to request it later. The file name on the server side is the SHA-1 of the file contents (without extension).
We could simply make the browser send a GET request to a URL like
/downloadfile.rem?fileID=37452834576542345676234?mimeType=text$plain?fileName=data.csv
and on the server side return a Content-Disposition header with "attachment; filename=...", but there is a bug in a certain version of IE 6 (that we have to support), that ignores the filename part of the header and the user will be requested to save a downloadfile.rem file.
Therefore our URL must end with the filename that should be given to the file.