1

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

4

2 回答 2

2

设置Response.ContentType = "application/octet-stream";通常对我有用。我也用Response.BinaryWrite。有一种 MIME 类型application/pdfWindows Internet Explorer 中的 MIME 类型检测),所以您也可以尝试一下。

application/pdf这是一个使用Microsoft的示例(如何使用 ASP.NET 和 Visual C# .NET 将二进制文件写入浏览器

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();
}
于 2012-09-10T12:12:02.840 回答
0

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

于 2013-04-10T04:03:58.033 回答