2017-08-07 47 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