【问题标题】:ASP.NET file download from server从服务器下载 ASP.NET 文件
【发布时间】:2013-08-30 21:45:38
【问题描述】:

用户单击按钮后,我希望下载文件。我尝试了以下似乎可行的方法,但并非没有抛出不可接受的异常(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();  

【问题讨论】:

标签: c# asp.net


【解决方案1】:

您可以使用 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");

然后在实际的处理程序代码中你可以使用HttpContext中的Request对象来获取查询字符串变量值,像这样:

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

// Use the yourVariableValue here

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

【讨论】:

  • 感谢您的回复。我想将文件夹位置作为参数传递,因此在查询中传递它不是一个好主意。有没有其他方法,我们可以传递值?
  • @KarlAnderson 是否可以通过 $.ajax 方法使用它?我有一个单页应用程序,我需要使用这种通过 asp.net 处理程序下载文件的方法。你能帮忙吗?
  • 为什么是“;”在文件名之后。 response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ";");
  • .aspx 与 .ashx 的任何区别。我有一个使用.aspx 和独立页面下载的示例,但在将网站包装到另一个页面时不起作用
  • 我在Server.MapPath() 中遇到错误:Server 在事件处理程序中
【解决方案2】:

尝试使用这组代码从服务器下载 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();

【讨论】:

    【解决方案3】:

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

    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();
    

    【讨论】:

      【解决方案4】:

      在 Karl Anderson 解决方案的基础上,您可以将参数放入会话信息中,然后在 response.TransmitFile(Server.MapPath( Session(currentSessionItemName))); 之后清除它们。

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

      【讨论】:

        【解决方案5】:
        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();
        
        
        }
        

        【讨论】:

        • 如果你使用网络表单,按钮必须有一个回发触发器
        • WriteFile 与 TransmitFile?
        【解决方案6】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-02-04
          • 2012-06-26
          • 2021-09-28
          • 1970-01-01
          相关资源
          最近更新 更多