在asp.net中显示保存文件对话框

时间:2012-09-10 12:09:39

标签: asp.net

这是在asp.net中。 我正在参考用户的输入创建pdf文件。 此pdf文件保存在服务器上的文件夹中。 现在这个文件可以使用response.redirect在浏览器中显示。 但我想向用户显示保存文件对话框(就像我们从网站下载exe时得到的那样),以便下载并将相同的文件保存在本地硬盘中,而不是在浏览器中打开它。 我怎么能这样做?

2 个答案:

答案 0 :(得分:2)

设置Response.ContentType = "application/octet-stream";通常适合我。我也使用Response.BinaryWrite。有一个mime类型application/pdfMIME Type Detection in Windows Internet Explorer),所以你也可以尝试一下。

以下是使用Microsoft(How To Write Binary Files to the Browser Using ASP.NET and Visual C# .NET

中的application/pdf的示例
private void Page_Load(object sender, System.EventArgs e)
{
  //Set the appropriate ContentType.
  Response.ContentType = "Application/pdf";
  //Get the physical path to the file.
  string FilePath = MapPath("acrobat.pdf");
  //Write the file directly to the HTTP content output stream.
  Response.WriteFile(FilePath);
  Response.End();
}

答案 1 :(得分:0)

启用保存为下载时间的框是关于浏览器配置而不是关于代码.....尝试这种配置的chrome浏览器..转到设置>显示提前设置>在下载启用复选框“询问下载前保存每个文件的位置”

相关问题