繁体   English   中英

ASP.net下载文件在ASP.NET中使用HttpContext.Current.Response.TransmitFile

[英]ASP.net download file Using HttpContext.Current.Response.TransmitFile in ASP.NET

public void Downloadfile(string sFileName, string sFilePath)
{
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.ClearContent();
    HttpContext.Current.Response.ClearHeaders();
    HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STREAM";
    String Header = "Attachment; Filename=" + sFileName;
    HttpContext.Current.Response.AppendHeader("Content-Disposition", Header);
    HttpContext.Current.Response.AppendHeader("Cache-Control", "no-cache");
    System.IO.FileInfo Dfile = new System.IO.FileInfo(HttpContext.Current.Server.MapPath(sFilePath));
    HttpContext.Current.Response.TransmitFile(Dfile.FullName);
    HttpContext.Current.ApplicationInstance.CompleteRequest();
}

我有一个下载按钮,点击将返回调用并下载相应的文件下载,但有时文件返回的是detailt.aspx文件。 我不明白发生了什么。 我需要帮助。 非常感谢

这对我有用了一段时间了。

public void Downloadfile(string sFileName, string sFilePath)
{
    var file = new System.IO.FileInfo(sFilePath);

    Response.Clear();
    Response.AddHeader("Content-Disposition", "attachment; filename=" + sFileName);
    Response.AddHeader("Content-Length", file.Length.ToString(CultureInfo.InvariantCulture));
    Response.ContentType = "application/octet-stream";
    Response.WriteFile(file.FullName);
    Response.End();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM