I am using a ASP/.Net webpage and i want to upload a pdf file into a SQL Database as a binary I am uping the build in upload control, can you please suggest a way of doing this. I also need to no how to read the pdf back and display it in a web browser. I will be using linq to upload and query my sql database.
You can use the VARBINARY(MAX)
type in your database, create a LINQ to SQL mapping and use the Binary
type with the byte[]
type containing your PDF file's content.
You need to save the data to a BLOB in the database, then read it when you need it:
For displaying the PDF back to the user, with aspx, you need to use an HTTPHandler. You'll simply write the pdf's bytes out to the HTTP response, making sure to properly set the "content-type" header. Here's a forum discussion which describes the solution:
It is false you need an HttpHandler
for serving the PDF back to the user, you can do it with an empty .aspx page, something like this:
<%@ Page Language="C#" AutoEventWireup="false" CodeFile="SendPDF.aspx.vb" Inherits="SendPDF" %>
And SendPDF.aspx.vb
file will look something like this:
partial class EWTD : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
Response.ContentType = "application/pdf";
Response.BinaryWrite(GetPDF());
}
protected byte[] GetPDF()
{
// Here you will retrieve the PDF as an array of bytes
}
}
The code it might need some changes to make it work, but you can get the idea.