从服务器保存 html 文件

本文关键字:文件 html 保存 服务器 | 更新日期: 2023-09-27 18:31:43

public void SaveSofaXML(object s, EventArgs e)
{
    HttpResponse response = HttpContext.Current.Response;
    StreamReader streamReader = new StreamReader(
        Server.MapPath("~/SentinelOperationsUI/SoFaXML.html"));
    string text = streamReader.ReadToEnd();
    streamReader.Close();
    response.StatusCode = 200;
    response.ContentEncoding = Encoding.UTF32;
    response.AddHeader("content-disposition", "attachment; filename=test.html");
    response.AddHeader("Content-Transfer-Encoding", "binary");
    response.AddHeader("Content-Length", 
        response.ContentEncoding.GetByteCount(text).ToString());
    response.ContentType = "application-download";
}

我认为我走在正确的轨道上。但是当我尝试保存html文件(~100kb)时,该文件永远不会完成下载。我错过了一些必需的标题吗?谢谢

从服务器保存 html 文件

试试这个:

 Response.AppendHeader("content-disposition", "attachment; filename=test.html");
 Response.TransmitFile(Server.MapPath("~/SentinelOperationsUI/SoFaXML.html"));
 Response.End();