If Adobe Reader (or equivalent) is not installed the user may see a save/open dialog. To display a pdf document in a browser and prevent the dialog from being shown use the following example code.
var fullURL = "http://www.somedomain.com?id=<some_id>"
var client= new System.Net.WebClient();
Byte[] buffer = client.DownloadData(fullURL);
if (buffer != null)
{
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.Clear();
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);
Response.End();
}
This is particularly useful when displaying pdf data read from the database.
reader = objDBCommand.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
int columnIndex = reader.GetOrdinal(*ColumnNameHere*);
returnData = (byte[])reader.GetValue(columnIndex);
}
}
returnData now contains the byte array of PDF data to be written out.
[UPDATE 1st July 2015]
In the above example the pdf data is retrieved from a URL. If the file already exists or downloaded file is saved locally then the Response methods change slightly. You can use AppendHeader() instead of AddHeader() and TransmitFile() instead of BinaryWrite().
string filePath = *FilePathHere*; string fileName = Path.GetFileName(filePath); ... HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;Filename=" + fileName); HttpContext.Current.Response.TransmitFile(filePath); ...

You must be logged in to post a comment.