[英]Error while downloading file using ASP.net c#
下面是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";
}
}
簡短的答案是,由於WebDev開發Web服務器不支持集成管道模式,因此您無法執行此操作。 解決方案是使用IIS Express托管頁面。 您仍然可以獲得完整的調試集成,依此類推。
方便的鏈接:
IIS Express 7.5 Support
部分) 或只是google VS2010 IIS Express
以獲取許多其他有用的提示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.