2017-08-07 10 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ファイルをダウンロードできます。

私を助けてくれてありがとう。

これはダウンロードする応答がない更新されたコードです。
EDITED CODE

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

MIMEタイプを追加するにはIISでチェックしましたか? – 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

関連する問題