Aspx页面从Web服务器上的特定目录下载任何类型的文件

时间:2012-03-29 14:24:24

标签: c# javascript asp.net .net iis

我的客户很快要求他将一些文件存储在Web服务器的文件夹中(我们有一个临时文件夹,它有权允许每个人)。

他希望直接将文件放入其中并提供链接,例如http://www.abcd.com/temp/somefile.rdl给他的客户直接下载文件。

是否有可用的现成aspx页面,以便我可以使用文件夹的页面集路径,它应该以这种方式工作。

如果没有,我可以仅使用aspx页面快速创建它。

我几乎用aspx

中的以下代码完成了它
<%
Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + Server.MapPath("~/TempReport/"+Request.QueryString["file"]) + "\"");
Response.ContentType = "Application/cab";
Response.TransmitFile(Server.MapPath("~/TempReport/"+Request.QueryString["file"]));
Response.End();
%>

唯一的问题是我只能按照以下行来下载cab类型的文件:

Response.ContentType = "Application/cab";

我想要下载任何类型的文件。

1 个答案:

答案 0 :(得分:1)

查看此帖子以获取答案,以获取应为您的内容类型

设置的MIME类型

Using .NET, how can you find the mime type of a file based on the file signature not the extension

或者你可以放,这应该可以正常下载二进制文件。 Response.ContentType =“application / octet-stream”;

        Page.Response.ContentType = "application/octet-stream"
        Page.Response.AddHeader("Content-Disposition", "attachment; filename=""" & strFilename & """")
        Page.Response.OutputStream.Write(objAttachment.Data, 0, objAttachment.Data.Length)