我已嘗試使用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();
你在IIS上檢查過添加MIME類型嗎? – Jacky
[「您正在請求的頁面由於擴展配置而無法投放」。錯誤消息](https://stackoverflow.com/questions/4388066/the-page-you-are-requesting-cannot-be-served-because-of-the-extension-configura) – BACON