【发布时间】:2014-07-16 09:27:39
【问题描述】:
下面是 AJAX 发送 POST 请求的代码
基本上我想在页面加载时从某个位置(完整路径)下载文件
但是当我调用这个页面时没有抛出错误页面成功调用 即使我得到 file.Exist=true ,也会创建响应但在浏览器上没有任何反应 我期待文件下载对话框。在进一步调试中,我在 Response 对象中发现异常 Headers = 'Response.Headers' 引发了“System.PlatformNotSupportedException”类型的异常 此操作需要 IIS 集成管道模式
我在我的本地主机(Visual Studio 开发服务器)中运行这个网页。 .net4 和 VS2010
页面加载代码:
protected void Page_Load(object sender, EventArgs e)
{
string path = Request.Form["path"];
// string reportName=path.Substring( path.LastIndexOf("\\") ,path.Length);
DAL.IO_helper i = new DAL.IO_helper();
string fullpath = i.GetReportsPath() + path;
FileInfo report = new FileInfo(fullpath);
if (report.Exists)
{
Response.ClearContent();
Response.ClearHeaders();
// Add the file name and attachment, which will force the open/cancel/save dialog box to show, to the header
Response.AddHeader("Content-Disposition", "attachment; filename=" + report.Name);
// Add the file size into the response header
Response.AddHeader("Content-Length", report.Length.ToString());
// Set the ContentType
Response.ContentType = ReturnExtension(report.Extension.ToLower());
// Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
Response.TransmitFile(report.FullName);
//Transmit file
Response.Flush();
// End the response
Response.End();
}
else
{
Response.ClearContent();
// End the response
Response.End();
}
}
功能
private string ReturnExtension(string fileExtension)
{
switch (fileExtension)
{
case ".txt":
return "text/plain";
case ".dat":
return "text/plain";
case ".doc":
return "application/ms-word";
case ".docx":
return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
case ".zip":
return "application/zip";
case ".xls":
return "application/vnd.ms-excel";
case ".xlsx":
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
case ".csv":
return "application/vnd.ms-excel";
case ".pdf":
return "application/pdf";
case ".xml":
return "application/xml";
case ".sdxl":
return "application/xml";
default:
return "application/octet-stream";
}
}
【问题讨论】:
-
@user1666620 我正在使用 Visual Studio 封装服务器
-
当你从 VS 启动它时使用 iis express 来运行站点。