views:

1160

answers:

3

I'm developing a webapp where the user is given the chance to upload his resume in pdf format. I'm using NHibernate as a data mapper and MS SQL SERVER 2005.

I want to be able to save the .pdf file to a given table... any ideas?

Thank you very much!

A: 

INSERT INTO Table (DocumentId, PDF) VALUES (newid(), 0x12345667)

where the hex constant is the hex-encoded bytestream of the PDF.

I'm sure someone can find a better way, but this is obvious to me.

Joshua
A: 

Any reason you need to put the PDF into the table? You could save the file to the filesystem and reference it with an ID. That would save you some SQL Server space. I don't believe you can index binary data anyways.

Abyss Knight
+3  A: 

We are doing exactly that with the "original" Java Hibernate3. You just map a byte array property of your persistable class to an column of Type "image".

package com.hibernate.pdf.sample;

public class TPDFDocument implements java.io.Serializable {


        private Integer pdfDocumentId;
        private byte[] document;


        public Integer getPdfDocumentId() {
            return this.pdfDocumentId;
        }

        public void setPdfDocumentId(Integer pdfDocumentId) {
            this.pdfDocumentId = pdfDocumentId;
        }

        public byte[] getDocument() {
            return this.document;
        }

        public void setDocument(byte[] document) {
            this.document = document;
        }

}

Hibernate Mapping:

<hibernate-mapping>
    <class name="com.hibernate.pdf.sample.TPDFDocument" table="T_PDFDocument">
        <id name="pdfDocumentId" type="integer">
            <column name="pdfDocumentId" />
            <generator class="identity" />
        </id>
        <property name="document" type="binary">
            <column name="document" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

Table creation:

CREATE TABLE [dbo].[T_PDFDocument](
    [pdfDocumentId] [int] IDENTITY(1,1) NOT NULL,
    [document] [image] NOT NULL,
CONSTRAINT [PK_PDFDocument] PRIMARY KEY CLUSTERED 
(
    [pdfDocumentId] ASC
)

All you have to do is to read the documents raw bytes into the array and persist it. In our situation the documents will get hardly larger than 1MB , so putting the whole thing into the byte-array won't cause performance issues. Maybe this solution isn't feasable for very large documents.

I guess with NHibernate implementation and C# the solution will look very similar.

huo73