2017-08-07 86 views
0

我已尝试使用Response.Redirect为用户下载.dat文件的代码。 但是,它总是让我看到这个错误。下载.dat文件的代码

The page you are requesting cannot be served because of the extension 
configuration. If the page is a script, add a handler. If the file should be 
downloaded, add a MIME map. 

对于Response.Redirect的

Response.Redirect(filepath, false); 

我的示例代码,我不没有什么不妥的地方。另外,我可以使用此代码下载.xlsx文件。

感谢您的帮助。

这是更新的代码,没有响应下载。
编辑的代码

string fpath = Server.MapPath("data") + "\\" + filename + ".dat"; 
if (File.Exists(fpath)) 
{ 
File.Delete(fpath); 
} 
StreamWriter swExtLogFile = new StreamWriter(fpath, true); 
try 
{ 
int i; 
foreach (DataRow row in tbl.Rows) 
{ 
object[] array = row.ItemArray; 
for (i = 0; i < array.Length - 1; i++) 
{ 
swExtLogFile.Write(array[i].ToString()); 
} 
swExtLogFile.WriteLine(array[i].ToString()); 
} 
swExtLogFile.Close(); 
swExtLogFile.Dispose(); 
FileInfo fInfo = new FileInfo(fpath); 
Response.Clear(); 
Response.ContentType = "application/octet-stream"; 
Response.AddHeader("Content-Length", fInfo.Length.ToString()); 
Response.AppendHeader("content-disposition", 
string.Format("attachment;filename={0}", fInfo.Name)); 
Response.Flush(); 
Response.TransmitFile(fInfo.FullName); 
HttpContext.Current.ApplicationInstance.CompleteRequest();  
+0

你在IIS上检查过添加MIME类型吗? – Jacky

+1

[“您正在请求的页面由于扩展配置而无法投放”。错误消息](https://stackoverflow.com/questions/4388066/the-page-you-are-requesting-cannot-be-served-because-of-the-extension-configura) – BACON

回答

0

试试这个

你不必做MIME地图。

private void dwnldFile(string filepath) 
{ 
    FileInfo fInfo = new FileInfo(filepath); 
    Response.Clear(); 
    Response.ContentType = "application/octet-stream"; 
    Response.AddHeader("Content-Length", fInfo.Length.ToString); 
    Response.AddHeader("content-disposition", string.Format("attachment;filename={0}", fInfo.Name)); 
    Response.Flush(); 
    Response.TransmitFile(fInfo.FullName) 
    Response.End(); 
} 
+0

感谢您回复我。 但添加代码后引发此错误 System.Threading.ThreadAbortException:线程正在中止。 此错误之前也已显示,但我实际上不知道是什么导致它。 – Ling

+0

你必须展示你的代码才能更好地理解。 – Sankar

+0

System.Threading.ThreadAbortException:线程正在中止。 来自Response.End() – Ling