2016-07-28 7 views
0

このコードを使用してファイルをダウンロードしていますが、エラーが発生します。それを手伝ってください。なぜ私のコードはスレッドを中止しましたか?

スレッドが中止されました。

protected void Download_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     string filePath = Convert.ToString(Attachment); 
     string fullFilePath = ("../../SiteImages/" + "donald.jpg"); 
     Response.Clear(); 
     Response.ClearHeaders(); 
     Response.ClearContent(); 
     Response.AddHeader("Content-Disposition", "attachment; filename=\"" + Path.GetFileName(fullFilePath) + "\""); 
     Response.ContentType = ContentType; 
     Response.TransmitFile(fullFilePath); 
     //MngLogs.InsertAuditsInfo("Tender downloaded via" + " " + MngLogs.PageName, MngLogs.UserMacAddress, MngLogs.UserIPAddress, UserID, "Download"); 
     //Response.End(); 
    } 
    catch (Exception ex) 
    { 
     Utility.Msg_Error(Master, ex.Message); 
    } 
} 
+0

どこでエラーが発生しましたか? 'Reponse.End()'は常に 'ThreadAbortException'をスローします。 [this](http://stackoverflow.com/questions/20988445/how-to-avoid-response-end-thread-was-being-aborted-exception-during-the-exce)と[this]を見てください。 (http://stackoverflow.com/questions/5834049/what-c​​auses-thread-was-being-aborted-exception-to-happen-at-random-and-showthth)post。 –

+0

同じ場所と私はそれを削除しましたが、まだファイルをダウンロードしません – Cuckoo

+0

とエラーをスローしませんが、まだファイルをダウンロードしません – Cuckoo

答えて

0

このダウンロード方法は動作しますか?

try 
{ 
    using (var client = new WebClient()) 
    { 
     client.DownloadFile(urlToFileOnInternet, pathToFileOnComputer); 
    } 
} 
catch (Exception ex) 
{ 
    Utility.Msg_Error(Master, ex.Message); 
} 

これが役に立ちます。

+0

何がurlToFileOnInternetですか? – Cuckoo

+0

ファイルをダウンロードしようとしていませんか?引用:「このコードを使用してファイルをダウンロードしています」 – MasterXD

+0

webserverから自分のフォルダにファイルをダウンロードします – Cuckoo

0

これと

Response.End(); 

を交換してください:

HttpContext.Current.Response.Flush(); 
HttpContext.Current.Response.SuppressContent = true; 
HttpContext.Current.ApplicationInstance.CompleteRequest(); 

Response.End();それは、現在のスレッドを中止します原因常に例外をスローします。この動作について詳しくは、Is Response.End() considered harmful?How to Avoid Response.End() "Thread was being aborted" Exception during the Excel file downloadをご覧ください。

+0

まだ動作していない – Cuckoo

+0

@Cuckooは私のアップデートをチェックします。 – Lesmian

関連する問題