473,778 Members | 1,934 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.PostedFi le

Dim strFile As String = myfile.Value

Dim nFileLen As Integer = mf.ContentLengt h

Dim b(nFileLen) As Byte

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

Dim str As String = Encoding.Unicod e.GetChars(b)

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

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

'Dim b1() As Byte

'b1 = Encoding.Unicod e.GetBytes(str)

'Response.Conte ntType = "applicatio n/pdf"

'Response.Binar yWrite(b1)

'Response.Flush ()

'Response.Close ()

'save file to DB

Dim cn As New SqlClient.SqlCo nnection("serve r=(local);initi al
catalog=atest;i ntegrated security=SSPI;" )

cn.Open()

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

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

cmd.ExecuteNonQ uery()

...........
Dim cmd As New SqlCommand("sel ect name,info from cust where nume='" &
cboFis.Selected Value & "'", cn)

Dim dr As SqlDataReader

dr = cmd.ExecuteRead er()

Dim str As String

Dim b() As Byte

If dr.Read() Then

str = dr("info")

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

b = Encoding.Unicod e.GetBytes(str)

Response.ClearC ontent()

Response.ClearH eaders()

Response.Conten tType = "applicatio n/pdf"

Response.Binary Write(b)

Response.Flush( )

Response.Close( )

End If

Nov 17 '05 #1
3 16263
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*****@videot ron.ca> wrote in message
news:O9******** ******@TK2MSFTN GP10.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.PostedFi le

Dim strFile As String = myfile.Value

Dim nFileLen As Integer = mf.ContentLengt h

Dim b(nFileLen) As Byte

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

Dim str As String = Encoding.Unicod e.GetChars(b)

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

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

'Dim b1() As Byte

'b1 = Encoding.Unicod e.GetBytes(str)

'Response.Conte ntType = "applicatio n/pdf"

'Response.Binar yWrite(b1)

'Response.Flush ()

'Response.Close ()

'save file to DB

Dim cn As New SqlClient.SqlCo nnection("serve r=(local);initi al
catalog=atest;i ntegrated security=SSPI;" )

cn.Open()

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

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

cmd.ExecuteNonQ uery()

..........
Dim cmd As New SqlCommand("sel ect name,info from cust where nume='" &
cboFis.Selected Value & "'", cn)

Dim dr As SqlDataReader

dr = cmd.ExecuteRead er()

Dim str As String

Dim b() As Byte

If dr.Read() Then

str = dr("info")

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

b = Encoding.Unicod e.GetBytes(str)

Response.ClearC ontent()

Response.ClearH eaders()

Response.Conten tType = "applicatio n/pdf"

Response.Binary Write(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*****@videot ron.ca> wrote in message
news:O9******** ******@TK2MSFTN GP10.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.PostedFi le

Dim strFile As String = myfile.Value

Dim nFileLen As Integer = mf.ContentLengt h

Dim b(nFileLen) As Byte

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

Dim str As String = Encoding.Unicod e.GetChars(b)

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

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

'Dim b1() As Byte

'b1 = Encoding.Unicod e.GetBytes(str)

'Response.Conte ntType = "applicatio n/pdf"

'Response.Binar yWrite(b1)

'Response.Flush ()

'Response.Close ()

'save file to DB

Dim cn As New SqlClient.SqlCo nnection("serve r=(local);initi al
catalog=atest;i ntegrated security=SSPI;" )

cn.Open()

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

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

cmd.ExecuteNonQ uery()

..........
Dim cmd As New SqlCommand("sel ect name,info from cust where nume='" &
cboFis.Selected Value & "'", cn)

Dim dr As SqlDataReader

dr = cmd.ExecuteRead er()

Dim str As String

Dim b() As Byte

If dr.Read() Then

str = dr("info")

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

b = Encoding.Unicod e.GetBytes(str)

Response.ClearC ontent()

Response.ClearH eaders()

Response.Conten tType = "applicatio n/pdf"

Response.Binary Write(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
12376
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 button i have to save the file in a shared directory on the web server by a unique name. Is it possible ?
6
11259
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 = Server.CreateObject("Scripting.FileSystemObject") fso.MoveFile Server.MapPath("/document/&session("nome_file")&"), _ Server.MapPath("/document/&session("id_risp")&"_"&session("id_recl")&")
0
1681
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 Image file on the local machine. Giving some code snippet below. RequestAPI = CType(WebRequest.Create(URLName), HttpWebRequest)
5
3146
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 have tried POSTin to be able to view the image, then saving it, but it doesnt work, the problem i have is that all the files on the server have the same name, which is http://1.1.1.1/DH/repository/content.tif and for me to be able to view one, i...
4
6724
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 code ?? thank you Pedro Leite From Portugal ------------------------------------
6
6455
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) and be able to load them to an image webcontrol, but system.web.ui.webcontrols.image only seems to have a control to load the image from a URL. There's no way to load this directly without saving the image as a file and then using...
1
2039
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 was created via an existing .sql file. Included were a bunch of stored procedures. I went in to re-format these procedures. (They were written in haste by another programmer. It is my task to go back and improve their readabilities.) However, I am...
6
3366
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 a user without saving it to the disk first. Obviously saving the file first and streaming it afterwards is fairly easy to do, but it relies on disk write permissions on the server and you'd have to make sure the file was removed afterwards.
3
2471
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 the binary data for the file. I can sucessfully take the files out of the databse again and display them in a data grid. I would like to resize the submitted file to a fixed size (say 180 x 120) before I upload it to the database and do this without...
10
7440
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
9629
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10068
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9923
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8954
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7474
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5497
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4031
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2863
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.