gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:44:49 24 4
gpt4 key购买 nike

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

关于ASP.net 下载文件 在 ASP.NET 中使用 HttpContext.Current.Response.TransmitFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19079591/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com