tags:

views:

2346

answers:

4

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.

+2  A: 

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.

Omer van Kloeten
Thanks, Do you have any examples or links?
http://blogs.microsoft.co.il/blogs/bursteg/archive/2008/05/12/linq-to-sql-filestream.aspx
Omer van Kloeten
Remember that Linq to Sql is new in .NET Framework 3.5, so this is not a viable solution in older versions
Rune Grimstad
@Rune: FTQ: "I will be using linq to upload and query my sql database."
Omer van Kloeten
+1  A: 

You need to save the data to a BLOB in the database, then read it when you need it:

http://www.akadia.com/services/dotnet_read_write_blob.html

Ady
A: 

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:

http://forums.asp.net/p/1120590/1750793.aspx#1750793

Jeb
+1  A: 

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.

Leandro López
Implementing this with HttpHandler (or even better, IHttpAsyncHandler, to asynchronously read the PDF) will perform much better, because it won't require creating a Page object (and going through page life cycle every time).
hmemcpy
That's true and I agree with you. However I was trying to make him notice is not necessary to build an IHttpHandler.
Leandro López