我正在使用此代码下载文件,但它会抛出错误。请帮我处理。
线程正在中止。
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 :(得分:0)
此下载方法是否可行?
try
{
using (var client = new WebClient())
{
client.DownloadFile(urlToFileOnInternet, pathToFileOnComputer);
}
}
catch (Exception ex)
{
Utility.Msg_Error(Master, ex.Message);
}
希望这有帮助。
答案 1 :(得分: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。