3
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文件。我不明白发生了什么。我需要帮助。非常感谢

4

1 回答 1

7

这对我来说已经有一段时间没有问题了。

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();
}
于 2013-09-30T00:57:19.540 回答