3

Can anyone suggest the best way to offer a file for download within a SharePoint web part? The file will be dynamically created on request, but I still need to end up with the standard page being displayed as well as the file being downloaded.

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
  • What do you mean by 'standard page'? – Alex Angas Sep 10 '09 at 11:24
  • I mean whatever the page displays when not handling the file download. For example, I have several tables on the page, and offer an 'export' option which hands the user the data as a .csv. However, I don't want the user to end up on a blank page et al. All of the examples I have seen for this only handle the file download and assume the file download is occuring from a dedicated page which is disposable. –  Sep 10 '09 at 11:43

2 Answers2

0

The basic ingredients of this example are quite simple and can be found in a Microsoft article here.

The Microsoft article includes the following code snippet:

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();
}

That should help you.

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Chris Jones
  • 2,630
  • 2
  • 21
  • 34
  • Unless I am mistaken, the problem with that is the 'Response.End' - you can't do anything other than send the file and thats that. I need to send the file and display a normal page. –  Sep 14 '09 at 11:48
0

In the end I dynamically added an iFrame to the web part during the reload after the user chooses the 'Export' option, and within that loaded a standard .aspx page which handles the sending. This got around the issue of having to send a Response.End