I have a web application allowing to download files from the server. Documents might be Office files (.docx, pptx, .xslx) or Pdf.
The logic is quite simple: on click from client side a web service is called and calls an aspx page (Print.aspx) passing the needed parameters. Print page calls a web service to retrieve the selected documents binaries and write them in the response:
protected void Page_Load(object sender, EventArgs e)
{
string fileName = Request.QueryString["fname"];
if (string.IsNullOrEmpty(documentID) == false)
{
byte[] document = GetPhysicalFile(documentID); //Get the binaries
showDownloadedDoc(document, fileName);
}
}
private void showDownloadedDoc(byte[] document, string fileName)
{
Response.Clear();
Response.ContentType = contentType;
Response.AppendHeader("content-disposition", string.Format("attachment; filename=\"{0}\"", fileName));
Response.BufferOutput = false;
Response.BinaryWrite(document);
Response.Close();
}
Pdf documents are opened in the Print.aspx page and the aspx page is loaded only once. For Office files, the Page_Load() method is invoked 3 times.
After the first time the dialog for Open/Save the document is opened and if “Open” ic clicked, then the Page_Load is called twice and after that the document is finally oepend in MS Word, for instance.
Is there a way I can prevent to have multiple page loads for opening these documents? I would like to avoid having to save the file on Server Side and Redirect to that URL, since a file for each access would be created.
Here is an example with a GenericHandler