473,503 Members | 9,836 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how can I save an image file to database?

how can I save an image file to database?

Nov 17 '05 #1
2 2154
Assuming a user is uploading the file, this code will save it into a database:

Private Sub btnAttach_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles btnAttach.Click

Dim iLength As Integer = _ CType(File1.PostedFile.InputStream.Length, Integer)

If iLength = 0 Then Exit Sub 'not a valid file

Dim sContentType As String = File1.PostedFile.ContentType

Dim sFileName As String, i As Integer

Dim bytContent As Byte()

ReDim bytContent(iLength) 'byte array, set to file size



'strip the path off the filename

i = InStrRev(File1.PostedFile.FileName.Trim, "\")

If i = 0 Then

sFileName = File1.PostedFile.FileName.Trim

Else

sFileName = Right(File1.PostedFile.FileName.Trim, Len(File1.PostedFile.FileName.Trim) - i)

End If



Try

File1.PostedFile.InputStream.Read(bytContent, 0, iLength)

With cmdInsertAttachment

.Parameters("@FileName").Value = sFileName

.Parameters("@FileSize").Value = iLength

.Parameters("@FileData").Value = bytContent

.Parameters("@ContentType").Value = sContentType

.ExecuteNonQuery()

End With

Catch ex As Exception

'Handle your database error here

dbConn.Close()

End Try

Response.Redirect(Request.Url.ToString) 'Refresh page

End Sub


Here's the SQL Query that the above code uses:
INSERT INTO tblAttachments

(FileName, FileSize, FileData, ContentType)

VALUES (@FileName, @FileSize, @FileData, @ContentType)

(Use an field type of Image for your SQL Server data field.)

Here's more info:

http://www.aspnetpro.com/features/20...200307so_f.asp

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
"Duy Nguyen" <it*****@hotmail.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
how can I save an image file to database?

Nov 17 '05 #2
thank you very much

"Steve C. Orr, MCSD" <St***@Orr.net> wrote in message
news:#G**************@TK2MSFTNGP09.phx.gbl...
Assuming a user is uploading the file, this code will save it into a
database:

Private Sub btnAttach_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnAttach.Click
Dim iLength As Integer = _ CType(File1.PostedFile.InputStream.Length,
Integer)
If iLength = 0 Then Exit Sub 'not a valid file
Dim sContentType As String = File1.PostedFile.ContentType
Dim sFileName As String, i As Integer
Dim bytContent As Byte()
ReDim bytContent(iLength) 'byte array, set to file size

'strip the path off the filename
i = InStrRev(File1.PostedFile.FileName.Trim, "\")
If i = 0 Then
sFileName = File1.PostedFile.FileName.Trim
Else
sFileName = Right(File1.PostedFile.FileName.Trim,
Len(File1.PostedFile.FileName.Trim) - i)
End If

Try
File1.PostedFile.InputStream.Read(bytContent, 0, iLength)
With cmdInsertAttachment
.Parameters("@FileName").Value = sFileName
.Parameters("@FileSize").Value = iLength
.Parameters("@FileData").Value = bytContent
.Parameters("@ContentType").Value = sContentType
.ExecuteNonQuery()
End With
Catch ex As Exception
'Handle your database error here
dbConn.Close()
End Try
Response.Redirect(Request.Url.ToString) 'Refresh page
End Sub

Here's the SQL Query that the above code uses:
INSERT INTO tblAttachments
(FileName, FileSize, FileData, ContentType)
VALUES (@FileName, @FileSize, @FileData, @ContentType)
(Use an field type of Image for your SQL Server data field.)
Here's more info:
http://www.aspnetpro.com/features/20...200307so_f.asp

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
"Duy Nguyen" <it*****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
how can I save an image file to database?
Nov 17 '05 #3

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

Similar topics

1
4476
by: Sam Martin | last post by:
Hi all, got a problem when converting a PNG to a JPG using the Image.Save(stream, format) method. When I save the original PNG to a file and open it, it's fine. I then save it to a database,...
4
1629
by: Al | last post by:
Hi I need to save lots of images on potentially very large database. Therefore the storage space is very crucial. I need what is most compact way to save an image. Currently I am using something...
1
6779
by: liuliuliu | last post by:
hi -- sorry if this is trivial -- but how do you make a screenshot of a pygame display? i have a surface which is basically the entire visible screen -- how do you write this surface as an image...
3
4390
by: =?Utf-8?B?Um9nZWxpbw==?= | last post by:
I'm having the wierdest problem. I'm trying to save images into a SQL 2005 database. the field is just called "IMAGES" and hte data type is "image". here is my code to save //following code...
4
6114
by: Logician | last post by:
I am running on my PC Windows Forms to collect data from websites, including images. I hit a problem with images and javascript, and I would appreciate any help. The current code fails with a...
4
5532
by: =?Utf-8?B?ZG1idXNv?= | last post by:
I have an Access database with a table named 'tblMedia'. In tblMedia I have a field named 'Media' that is defined as an 'OLE Ojbect' field. In this Media field, I have pictures, they are jpegs. I'm...
0
2487
by: hnpatel | last post by:
hello friends, i m making application in vb.NET. i m using MY SQL 5.0 and vs 2005 for this application. Now I want to save image file in sql database. i m using blob datatype of image field. i...
5
7587
by: countnazgul | last post by:
Hello, I've got very odd problem. I use windows application to store files in data base (MySql) and then to extract this files from data base and save it on hdd. But when i save them back to hdd...
1
4860
by: chennaibala | last post by:
can any one send me mutiple image upload program and save the file name with extension in mysql table.we must cheak uploaded file type like bmp or any image file while uploading. i develop...
0
7207
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,...
0
7095
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7361
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...
1
7015
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...
0
7470
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...
1
5026
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...
0
4693
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3183
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...
1
749
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.