[英]ASP.NET file download from server
用户单击按钮后,我想要下载一个文件。 我尝试了以下似乎有效的方法,但并非没有抛出不可接受的异常(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) 下载文件,如下所示:
下载文件.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
注意- 通常将文件名作为查询字符串参数传递给用户,以向用户建议文件的实际内容,在这种情况下,他们可以使用另存为...
试试这组代码从服务器下载一个 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();
进行如下更改并将服务器内容类型重新部署为
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 解决方案,您可以将参数放入会话信息中,然后在response.TransmitFile(Server.MapPath( Session(currentSessionItemName)));
后清除它们response.TransmitFile(Server.MapPath( Session(currentSessionItemName)));
.
有关会话的更多信息,请参阅 MSDN 页面HttpSessionState.Add Method (String, Object) 。
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();
}
从服务器下载文件的简单解决方案:
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.