从服务器上下载ASP.NET文件

用户点击一个button后,我想要一个文件被下载。 我试过以下似乎工作,但不是没有抛出一个exception(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(); 

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

DownloadFile.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; } } } 

然后,您可以从button单击事件处理程序调用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"); } 

将parameter passing给HTTP处理程序:

您可以简单地将查询stringvariables追加到Response.Redirect() ,如下所示:

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

然后在实际的处理程序代码中,可以使用HttpContextRequest对象来获取查询stringvariables值,如下所示:

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

注意 – 通常将文件名作为查询stringparameter passing给用户,以向用户build议文件实际是什么,在这种情况下,他们可以使用“另存为”来覆盖该名称值。

试试这组代码从服务器下载一个CSV文件。

 byte[] Content= File.ReadAllBytes(FilePath) Response.ContentType = "text/csv"; Response.AddHeader("content-disposition", "attachment; filename=" + fileName + ".csv"); Response.BufferOutput = true; Response.OutputStream.Write(Content, 0, Content.Length); Response.End(); 

按照以下所述进行更改,并在服务器内容types上重新部署

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

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

有关会话的更多信息请参阅MSDN页面HttpSessionState.Add方法(string,对象)

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

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

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