Thursday, February 14, 2013

Save dialog shows for a generated PDF

I guess this is in general Request/Response object, but I was on SharePoint 2013 environment.

There is a link in the application which needed to open automatically and was working fine. But when user opens the document and tries to close it without modifying anything, a Save dialog was popping up. So here is the code to open the document automatically based on ActiveX plug in on the users' system. (Generating the document ommitted.)
//Do not buffer
this.Page.Response.Buffer = false;

this.Page.Response.ClearContent();
this.Page.Response.ClearHeaders();
this.Page.Response.AppendHeader("Content-Disposition", "inline; filename=" + fileName.ToString());
this.Page.Response.ContentType = strContentType;

//Check that the client is connected and has not closed the connection after the request
if (this.Page.Response.IsClientConnected)
{
     this.Page.Response.BinaryWrite(fileContents);
}

//No need to flush or close as buffering is disabled
this.Page.Response.Flush();
this.Page.Response.Close();


As in the code, if Content-Disposition is set to "inline" document is automatically opened (in my case its a PDF reader), or if it is set to attachment, user will be popped with a Open/Save dialog box.

NOW

After opening the document, even if the user did not change anything, "do you want to save?" sort of dialog was displayed. In the code, at first, I did not put Response.Flush() and Response.Close() because Response was not buffered (note this.Page.Response.Buffer = false in the code). So when I put Response.Flush() and Respose.Close() back in, Save dialog was gone.

Hope this helps!!!

No comments:

Post a Comment