44

用户单击按钮后,我想要下载文件。我尝试了以下似乎可行的方法,但并非没有抛出不可接受的异常(ThreadAbort)。

    System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
    response.ClearContent();
    response.Clear();
    response.ContentType = "text/plain";
    response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ";");
    response.TransmitFile(Server.MapPath("FileDownload.csv"));
    response.Flush();
    response.End();  
4

6 回答 6

85

您可以使用 HTTP 处理程序 (.ashx) 下载文件,如下所示:

下载文件.ashx:

public class DownloadFile : IHttpHandler 
{
    public void ProcessRequest(HttpContext context)
    {   
        System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
        response.ClearContent();
        response.Clear();
        response.ContentType = "text/plain";
        response.AddHeader("Content-Disposition", 
                           "attachment; filename=" + fileName + ";");
        response.TransmitFile(Server.MapPath("FileDownload.csv"));
        response.Flush();    
        response.End();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

然后您可以从按钮单击事件处理程序中调用 HTTP 处理程序,如下所示:

标记:

<asp:Button ID="btnDownload" runat="server" Text="Download File" 
            OnClick="btnDownload_Click"/>

代码隐藏:

protected void btnDownload_Click(object sender, EventArgs e)
{
    Response.Redirect("PathToHttpHandler/DownloadFile.ashx");
}

将参数传递给 HTTP 处理程序:

您可以简单地将查询字符串变量附加到Response.Redirect(),如下所示:

Response.Redirect("PathToHttpHandler/DownloadFile.ashx?yourVariable=yourValue");

然后在实际的处理程序代码中,您可以使用 中的Request对象HttpContext来获取查询字符串变量值,如下所示:

System.Web.HttpRequest request = System.Web.HttpContext.Current.Request;
string yourVariableValue = request.QueryString["yourVariable"];

// Use the yourVariableValue here

注意- 通常将文件名作为查询字符串参数传递,以向用户建议文件实际是什么,在这种情况下,他们可以使用 Save As... 覆盖该名称值

于 2013-08-28T01:26:39.347 回答
11

尝试使用这组代码从服务器下载 CSV 文件。

byte[] Content= File.ReadAllBytes(FilePath); //missing ;
Response.ContentType = "text/csv";
Response.AddHeader("content-disposition", "attachment; filename=" + fileName + ".csv");
Response.BufferOutput = true;
Response.OutputStream.Write(Content, 0, Content.Length);
Response.End();
于 2013-08-28T05:16:22.073 回答
2

进行如下更改并将服务器内容类型重新部署为

Response.ContentType = "application/octet-stream";

这对我有用。

Response.Clear(); 
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name); 
Response.AddHeader("Content-Length", file.Length.ToString()); 
Response.ContentType = "application/octet-stream"; 
Response.WriteFile(file.FullName); 
Response.End();
于 2015-07-22T13:55:48.390 回答
1

对于 Karl Anderson 解决方案,您可以将参数放入会话信息中,然后在response.TransmitFile(Server.MapPath( Session(currentSessionItemName)));.

有关会话的更多信息,请参阅 MSDN 页面HttpSessionState.Add Method (String, Object)

于 2014-09-01T09:56:56.903 回答
0
protected void DescargarArchivo(string strRuta, string strFile)
{
    FileInfo ObjArchivo = new System.IO.FileInfo(strRuta);
    Response.Clear();
    Response.AddHeader("Content-Disposition", "attachment; filename=" + strFile);
    Response.AddHeader("Content-Length", ObjArchivo.Length.ToString());
    Response.ContentType = "application/pdf";
    Response.WriteFile(ObjArchivo.FullName);
    Response.End();


}
于 2015-04-28T14:31:26.480 回答
0

从服务器下载文件的简单解决方案:

protected void btnDownload_Click(object sender, EventArgs e)
        {
            string FileName = "Durgesh.jpg"; // It's a file name displayed on downloaded file on client side.

            System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
            response.ClearContent();
            response.Clear();
            response.ContentType = "image/jpeg";
            response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
            response.TransmitFile(Server.MapPath("~/File/001.jpg"));
            response.Flush();
            response.End();
        }
于 2016-03-17T17:51:51.183 回答