i want to display a PDF file or .DOC or an image saved in my Data Base
into my ASP.net page
If a file is saved in Database, it is normally in Binary format.
If so, you need a File Handler in order to display those Binary Data back to client
browser.
For example,
public class FileHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string id = context.Request.QueryString["id"];
// Let say you get the file data from database based on id
// ...
var fileData = new byte[] { ... };
string fileName = "PDF_FILENAME.pdf";
context.Response.Clear();
// Need to return appropriate ContentType for different type of file.
context.Response.ContentType = "application/pdf";
context.Response.AddHeader("Content-Disposition",
"attachment; filename=" + fileName);
context.Response.AddHeader("Content-Length", fileData.Length.ToString());
context.Response.Write(fileData);
context.Response.End();
}
public bool IsReusable
{
get { return false; }
}
}
Usage
<a href="/FileHandler.ashx?id=1">My File</a>