469,644 Members | 1,752 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,644 developers. It's quick & easy.

how can I save an image file to database?

how can I save an image file to database?

Nov 17 '05 #1
2 2029
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Sam Martin | last post: by
1 post views Thread by liuliuliu | last post: by
3 posts views Thread by =?Utf-8?B?Um9nZWxpbw==?= | last post: by
4 posts views Thread by Logician | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.