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);
    }
}
4

2 回答 2

0

这种下载方法可行吗?

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

希望这可以帮助。

于 2016-07-28T09:30:50.153 回答
0

代替

Response.End();

有了这个:

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

Response.End();总是抛出异常,因为它将中止当前线程。您可以在此处阅读有关此行为的更多信息:Response.End() 被认为是有害的吗?,如何避免 Response.End() Excel 文件下载过程中出现“线程被中止”异常

于 2016-07-28T09:33:56.647 回答