473,386 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

saving pdf file to sql server

Hi group,

I have an web application where the user can upload a pdf file. This file is
stored in a table, in a column of type ntext.
The user can later request the content of this column, and the application
should open a new page and load the pdf content.

My problem:
If I read the uploaded pdf file into a string variable and then I
response.write it right away to another page, it shows fine (I can see a pdf
document opened with acrobat reader).
But if I save the string in the sql database and then later retreive it from
there, it will not show anymore.
It seems like it is being somehow transformed during saving to/retreiving
from the sqlserver ...

Any ideas ?

thanks,
Andrei.

Following is the code I use. The commented block sends back the pdf info to
the browser.
Dim mf As HttpPostedFile = myfile.PostedFile

Dim strFile As String = myfile.Value

Dim nFileLen As Integer = mf.ContentLength

Dim b(nFileLen) As Byte

mf.InputStream.Read(b, 0, nFileLen)

Dim str As String = Encoding.Unicode.GetChars(b)

str = Replace(str, "'", "''")

'str = Replace(str, "''", "'")

'Dim b1() As Byte

'b1 = Encoding.Unicode.GetBytes(str)

'Response.ContentType = "application/pdf"

'Response.BinaryWrite(b1)

'Response.Flush()

'Response.Close()

'save file to DB

Dim cn As New SqlClient.SqlConnection("server=(local);initial
catalog=atest;integrated security=SSPI;")

cn.Open()

Dim cmd As New SqlClient.SqlCommand("insert into cust (name, info) values("
& _

"'" & strFile & "','" & str & "')", cn)

cmd.ExecuteNonQuery()

...........
Dim cmd As New SqlCommand("select name,info from cust where nume='" &
cboFis.SelectedValue & "'", cn)

Dim dr As SqlDataReader

dr = cmd.ExecuteReader()

Dim str As String

Dim b() As Byte

If dr.Read() Then

str = dr("info")

str = Replace(str, "''", "'")

b = Encoding.Unicode.GetBytes(str)

Response.ClearContent()

Response.ClearHeaders()

Response.ContentType = "application/pdf"

Response.BinaryWrite(b)

Response.Flush()

Response.Close()

End If

Nov 17 '05 #1
3 16210
Hi,

I think it is better to save the PDF file as binary data in the SQL
server instead of translating it from / to string.

Natty Gur, CTO
Dao2Com Ltd.
28th Baruch Hirsch st. Bnei-Brak
Israel , 51114

Phone Numbers:
Office: +972-(0)3-5786668
Fax: +972-(0)3-5703475
Mobile: +972-(0)58-888377

Know the overall picture
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 17 '05 #2
PJ
use the Image datatype in sqlserver. Put the .InputStream from the
PostedFile into a byte array. The byte array will be your parameter into
the stored proc that takes an image datatype. Careful if you happen to be
deriving parameters from the proc...as .net will not type the Image SqlType
correctly....I believe it makes it ntext or something....

~PJ

"andy_ro" <an*****@videotron.ca> wrote in message
news:O9**************@TK2MSFTNGP10.phx.gbl...
Hi group,

I have an web application where the user can upload a pdf file. This file is stored in a table, in a column of type ntext.
The user can later request the content of this column, and the application
should open a new page and load the pdf content.

My problem:
If I read the uploaded pdf file into a string variable and then I
response.write it right away to another page, it shows fine (I can see a pdf document opened with acrobat reader).
But if I save the string in the sql database and then later retreive it from there, it will not show anymore.
It seems like it is being somehow transformed during saving to/retreiving
from the sqlserver ...

Any ideas ?

thanks,
Andrei.

Following is the code I use. The commented block sends back the pdf info to the browser.
Dim mf As HttpPostedFile = myfile.PostedFile

Dim strFile As String = myfile.Value

Dim nFileLen As Integer = mf.ContentLength

Dim b(nFileLen) As Byte

mf.InputStream.Read(b, 0, nFileLen)

Dim str As String = Encoding.Unicode.GetChars(b)

str = Replace(str, "'", "''")

'str = Replace(str, "''", "'")

'Dim b1() As Byte

'b1 = Encoding.Unicode.GetBytes(str)

'Response.ContentType = "application/pdf"

'Response.BinaryWrite(b1)

'Response.Flush()

'Response.Close()

'save file to DB

Dim cn As New SqlClient.SqlConnection("server=(local);initial
catalog=atest;integrated security=SSPI;")

cn.Open()

Dim cmd As New SqlClient.SqlCommand("insert into cust (name, info) values(" & _

"'" & strFile & "','" & str & "')", cn)

cmd.ExecuteNonQuery()

..........
Dim cmd As New SqlCommand("select name,info from cust where nume='" &
cboFis.SelectedValue & "'", cn)

Dim dr As SqlDataReader

dr = cmd.ExecuteReader()

Dim str As String

Dim b() As Byte

If dr.Read() Then

str = dr("info")

str = Replace(str, "''", "'")

b = Encoding.Unicode.GetBytes(str)

Response.ClearContent()

Response.ClearHeaders()

Response.ContentType = "application/pdf"

Response.BinaryWrite(b)

Response.Flush()

Response.Close()

End If

Nov 17 '05 #3
Thank you, Natty and PJ,

I used an Image field in SQLServer with a stored procedure that writes to it
and it works perfect.

Andrei.

"andy_ro" <an*****@videotron.ca> wrote in message
news:O9**************@TK2MSFTNGP10.phx.gbl...
Hi group,

I have an web application where the user can upload a pdf file. This file is stored in a table, in a column of type ntext.
The user can later request the content of this column, and the application
should open a new page and load the pdf content.

My problem:
If I read the uploaded pdf file into a string variable and then I
response.write it right away to another page, it shows fine (I can see a pdf document opened with acrobat reader).
But if I save the string in the sql database and then later retreive it from there, it will not show anymore.
It seems like it is being somehow transformed during saving to/retreiving
from the sqlserver ...

Any ideas ?

thanks,
Andrei.

Following is the code I use. The commented block sends back the pdf info to the browser.
Dim mf As HttpPostedFile = myfile.PostedFile

Dim strFile As String = myfile.Value

Dim nFileLen As Integer = mf.ContentLength

Dim b(nFileLen) As Byte

mf.InputStream.Read(b, 0, nFileLen)

Dim str As String = Encoding.Unicode.GetChars(b)

str = Replace(str, "'", "''")

'str = Replace(str, "''", "'")

'Dim b1() As Byte

'b1 = Encoding.Unicode.GetBytes(str)

'Response.ContentType = "application/pdf"

'Response.BinaryWrite(b1)

'Response.Flush()

'Response.Close()

'save file to DB

Dim cn As New SqlClient.SqlConnection("server=(local);initial
catalog=atest;integrated security=SSPI;")

cn.Open()

Dim cmd As New SqlClient.SqlCommand("insert into cust (name, info) values(" & _

"'" & strFile & "','" & str & "')", cn)

cmd.ExecuteNonQuery()

..........
Dim cmd As New SqlCommand("select name,info from cust where nume='" &
cboFis.SelectedValue & "'", cn)

Dim dr As SqlDataReader

dr = cmd.ExecuteReader()

Dim str As String

Dim b() As Byte

If dr.Read() Then

str = dr("info")

str = Replace(str, "''", "'")

b = Encoding.Unicode.GetBytes(str)

Response.ClearContent()

Response.ClearHeaders()

Response.ContentType = "application/pdf"

Response.BinaryWrite(b)

Response.Flush()

Response.Close()

End If

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Vikram | last post by:
Hi, Given below is my task. An user can open the xls file from my website (loaded on the top frame). After filling the Excel, he can click a send button at the bottom frame. By clicking the...
6
by: tom | last post by:
Hi lads - got an issue with renaming a file. I'm actually using the FSO method 'MoveFile'.I did try to do my best, but doesn't want to work. this is the code: Dim fso set fso =...
0
by: Umesh | last post by:
Hi, I have an Application, in which 1) need to post data to a URL(Remote Server), by using HTTPRequest. 2) get the Image data in the form of Stream in Response. 3) need to save this stream as a...
5
by: Thaynann | last post by:
I have an app that (at the moment) moves through files that are on a web site, and deletes them, wat i want to do for the next stage, is to be able to download each file before i delete it. i...
4
by: Pedro Leite | last post by:
Good Afternoon. the code below is properly retreiving binary data from a database and saving it. but instead of saving at client machine is saving at the server machine. what is wrong with my...
6
by: Mark Denardo | last post by:
My question is similar to one someone posted a few months back, but I don't see any replies. Basically I want to be able to have users upload photos and save them in a database (as byte data)...
1
by: Allie | last post by:
Hi, all. This might be a silly question... but I am very new to programming in SQL so please bear with me :) So. I'm using MS SQL Server 2005 Management Studio Express. I have a table that...
6
by: Karl | last post by:
Hi all, It may seem like a rather odd request (or not) but I would like to be able to create a file (doc, jpg, xls or one of many other files that can be automated) on a website and stream it to...
3
by: pozze | last post by:
Hi, I've just made the change from ASP to .net. I have a file (code below) that saves a user submitted file to a MS SQL 2005 database. It collects the file name, file size, file type, and lastly...
10
by: Nathan Sokalski | last post by:
I am using ASP.NET 2.0, and need to know how to save and use an image that is stored in an SQL Server image datatype. How can I do this using ASP.NET? Thanks.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.