Showing Save file dialog box in asp.net

2020-04-30 03:49发布

问题:

This is in asp.net. I am creating pdf file with reference to the inputs from users. This pdf file is saved on folder on server. Now this is file can be shown in browser using response.redirect. But I want to show the Save file dialog (like we get while downloading exe from websites) to the user for downloading and saving the same file in local hard disk instead of opening it in browser. How can i do that??

回答1:

Setting Response.ContentType = "application/octet-stream"; usually works for me. I also use Response.BinaryWrite as well. There IS a mime type application/pdf (MIME Type Detection in Windows Internet Explorer), so you might give that a try too.

Here's an example using application/pdf from Microsoft (How To Write Binary Files to the Browser Using ASP.NET and 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();
}


回答2:

to enable the save as box on download time is the about browser configuration not about the code ..... try this configuration of chrome browser ..Goto setting > show advance setting > in Downloads enable check box of "Ask where to save each file before downloading"



标签: asp.net