|
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class streampdf : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//note there is no error handling written for this - there should be some if you are relying on it for security
/*
* hardcoded the filename - could also come from a database etc if you passed a parameter to represent the file
* You can then check eg through roles that the user is allowed to view the file
*/
String FilePath = "MynewPDF.pdf";
Response.ContentType = "application/pdf";
string sFile = Server.MapPath(FilePath);
FileStream fs = new FileStream(sFile, FileMode.Open, FileAccess.Read);
//check the file exists
if (fs.CanRead)
{
byte[] b = new byte[(int)fs.Length];
fs.Read(b, 0, (int)fs.Length);
fs.Close();
Response.Clear();
Response.BinaryWrite(b);
Response.End();
}
else
{
fs.Close();
//You should probably return some sort of message or perhaps display a PDF with an error message.
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.