473,770 Members | 4,718 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 2167
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.EventArg s) Handles btnAttach.Click

Dim iLength As Integer = _ CType(File1.Pos tedFile.InputSt ream.Length, Integer)

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

Dim sContentType As String = File1.PostedFil e.ContentType

Dim sFileName As String, i As Integer

Dim bytContent As Byte()

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



'strip the path off the filename

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

If i = 0 Then

sFileName = File1.PostedFil e.FileName.Trim

Else

sFileName = Right(File1.Pos tedFile.FileNam e.Trim, Len(File1.Poste dFile.FileName. Trim) - i)

End If



Try

File1.PostedFil e.InputStream.R ead(bytContent, 0, iLength)

With cmdInsertAttach ment

.Parameters("@F ileName").Value = sFileName

.Parameters("@F ileSize").Value = iLength

.Parameters("@F ileData").Value = bytContent

.Parameters("@C ontentType").Va lue = sContentType

.ExecuteNonQuer y()

End With

Catch ex As Exception

'Handle your database error here

dbConn.Close()

End Try

Response.Redire ct(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*****@hotmai l.com> wrote in message news:%2******** ********@TK2MSF TNGP09.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.ne t> wrote in message
news:#G******** ******@TK2MSFTN GP09.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.EventArg s) Handles btnAttach.Click
Dim iLength As Integer = _ CType(File1.Pos tedFile.InputSt ream.Length,
Integer)
If iLength = 0 Then Exit Sub 'not a valid file
Dim sContentType As String = File1.PostedFil e.ContentType
Dim sFileName As String, i As Integer
Dim bytContent As Byte()
ReDim bytContent(iLen gth) 'byte array, set to file size

'strip the path off the filename
i = InStrRev(File1. PostedFile.File Name.Trim, "\")
If i = 0 Then
sFileName = File1.PostedFil e.FileName.Trim
Else
sFileName = Right(File1.Pos tedFile.FileNam e.Trim,
Len(File1.Poste dFile.FileName. Trim) - i)
End If

Try
File1.PostedFil e.InputStream.R ead(bytContent, 0, iLength)
With cmdInsertAttach ment
.Parameters("@F ileName").Value = sFileName
.Parameters("@F ileSize").Value = iLength
.Parameters("@F ileData").Value = bytContent
.Parameters("@C ontentType").Va lue = sContentType
.ExecuteNonQuer y()
End With
Catch ex As Exception
'Handle your database error here
dbConn.Close()
End Try
Response.Redire ct(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*****@hotmai l.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.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
4493
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, retrieve it and Save it as a JPG format and most of the image is fine except a large black band at the bottom of the image. What can i do?
4
1650
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 like thi Thanks in advanc A dim IoImageValue() As Byt ….get the bytes from InkPictureBox control myDataRow("Graph") = IoImageValu
1
6826
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 file during specific events in the script execution? image format doesnt matter. thanks! christine
3
4400
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 saves an image as a byte array to a DB string strFn = this.openFileDialog1.FileName; this.pictureBox1.Image = Image.FromFile(strFn); FileInfo fiImage = new FileInfo(strFn); m_lImageFileLength = fiImage.Length;
4
6144
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 copy error. My Internet connected PC is not the development machine, so I cannot debug on that PC and my other PC has no Internet connection. So I cannot debug using .NET as normal.
4
5547
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 using VB.NET 2005 and I would like to open the Access database and write the OLE Objects to the file system. Here's what I got and it's not working. 'Open connection to the Access database OpenConnection()
0
2555
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 want to save image file from vb.net form. i m getting image in picturecontrol through openfiledialogbox. now how to save this image in my database through insert query? Plz help me for this..... Thanx in advance.....
5
7619
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 tihs files are 1Kb less from original file. When i insert pictures this 1kb obviously is not imported and the pictures is shown. But other files when try to be opened is corrupted or if they open some error message is shown. So the code that i use to...
1
4884
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 program,which can upload many file in folder.problem is,am unable to save my file name in to database.because i used same name for all input file type as file. i dont know get name of file.below i presented my coding for ur view.fed up with my coding..pls...
0
9595
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...
0
9432
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10008
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
9873
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
8891
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...
0
6682
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3974
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
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2822
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.