如何强制服务器上的文本文件的下载对话框?

时间:2010-10-09 15:38:30

标签: c# asp.net dialog download

如何强制服务器上的文本文件下载对话框?

当我使用打击代码时,对话窗口是针对aspx文件的......(为什么?)

    string FileBankPhysicalFolder = Server.MapPath("~/FileBanks/");
    string Name = "FileBank_" + "Melli_" + Session["Co_ID"].ToString() + "_" + RadcbDateOfPardakht.SelectedValue.Replace('/',',') + ".txt";
    string FileBankPath = FileBankPhysicalFolder + Name;
    string Content = Header + Body;
    System.IO.File.WriteAllText(FileBankPath, Content);

    Response.ContentType = "text/plain";
    Response.AppendHeader("Content-Disposition", "attachment;" +  Name);
    Response.WriteFile(FileBankPath);
    Response.End();

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

您应该发送强制下载标题以及该文件。我不知道你怎么会在ASP中这样做,但基本上你必须用一些ASP内置函数读取文件,然后将其输出到附加的浏览器

Content-Type: application/force-download;
Content-Disposition: attachment; filename=\yourfile.txt

根据您的代码判断:

Response.AppendHeader("Content-Type", "application/force-download;");
Response.AppendHeader("Content-Disposition", "attachment; filename="+ Name);

干杯!