473,396 Members | 1,966 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,396 software developers and data experts.

how to upload photo in visual basic

slapshock
gud morning!!!!!!!!

i have a problem on how to upload photo and save it in the database....
i am not so familiar in visual basic on how to code.....
can you help me with this problem??
i want the codes that can the uploading and saving photos in visual basics....




tnx
slapshock
Oct 8 '06 #1
4 13724
Hemant Pathak
92 Expert
To add a new record, the program lets the user select a graphic file. The program opens the file and reads it in blocks of binary data. For each block, the program uses AppendChunk to add the block to the database

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub mnuRecordAdd_Click()
  3. Dim rs As ADODB.Recordset
  4. Dim person_name As String
  5. Dim file_num As String
  6. Dim file_length As String
  7. Dim bytes() As Byte
  8. Dim num_blocks As Long
  9. Dim left_over As Long
  10. Dim block_num As Long
  11.  
  12.     person_name = InputBox("Name")
  13.     If Len(person_name) = 0 Then Exit Sub
  14.  
  15.     dlgPicture.Flags = _
  16.         cdlOFNFileMustExist Or _
  17.         cdlOFNHideReadOnly Or _
  18.         cdlOFNExplorer
  19.     dlgPicture.CancelError = True
  20.     dlgPicture.Filter = "Graphics " & _
  21.         "Files|*.bmp;*.ico;*.jpg;*.gif"
  22.  
  23.     On Error Resume Next
  24.     dlgPicture.ShowOpen
  25.     If Err.Number = cdlCancel Then
  26.         Exit Sub
  27.     ElseIf Err.Number <> 0 Then
  28.         MsgBox "Error " & Format$(Err.Number) & _
  29.             " selecting file." & vbCrLf & Err.Description
  30.         Exit Sub
  31.     End If
  32.  
  33.     ' Open the picture file.
  34.     file_num = FreeFile
  35.     Open dlgPicture.FileName For Binary Access Read As _
  36.         #file_num
  37.  
  38.     file_length = LOF(file_num)
  39.     If file_length > 0 Then
  40.         num_blocks = file_length / BLOCK_SIZE
  41.         left_over = file_length Mod BLOCK_SIZE
  42.  
  43.         Set rs = New ADODB.Recordset
  44.         rs.CursorType = adOpenKeyset
  45.         rs.LockType = adLockOptimistic
  46.         rs.Open "Select Name, Picture, FileLength FROM " & _
  47.             "People", m_DBConn
  48.  
  49.         rs.AddNew
  50.         rs!Name = person_name
  51.         rs!FileLength = file_length
  52.  
  53.         ReDim bytes(BLOCK_SIZE)
  54.         For block_num = 1 To num_blocks
  55.             Get #file_num, , bytes()
  56.             rs!Picture.AppendChunk bytes()
  57.         Next block_num
  58.  
  59.         If left_over > 0 Then
  60.             ReDim bytes(left_over)
  61.             Get #file_num, , bytes()
  62.             rs!Picture.AppendChunk bytes()
  63.         End If
  64.  
  65.         rs.Update
  66.         Close #file_num
  67.  
  68.         lstPeople.AddItem person_name
  69.         lstPeople.Text = person_name
  70.     End If
  71. End Sub
  72.  

To retreive a picture, the program selects the appropriate database record. It uses the TemporaryFileName function to get a temporary file name and opens the file. It then uses GetChunk to read the data from the database in blocks and writes the blocks into the file. The program then uses LoadPicture to load the file, and Kill to remove it.

Expand|Select|Wrap|Line Numbers
  1. ' Display the clicked person.
  2. Private Sub lstPeople_Click()
  3. Dim rs As ADODB.Recordset
  4. Dim bytes() As Byte
  5. Dim file_name As String
  6. Dim file_num As Integer
  7. Dim file_length As Long
  8. Dim num_blocks As Long
  9. Dim left_over As Long
  10. Dim block_num As Long
  11. Dim hgt As Single
  12.  
  13.     picPerson.Visible = False
  14.     Screen.MousePointer = vbHourglass
  15.     DoEvents
  16.  
  17.     ' Get the record.
  18.     Set rs = m_DBConn.Execute("SELECT * FROM People WHERE " & _
  19.         "Name='" & _
  20.         lstPeople.Text & "'", , adCmdText)
  21.     If rs.EOF Then Exit Sub
  22.  
  23.     ' Get a temporary file name.
  24.     file_name = TemporaryFileName()
  25.  
  26.     ' Open the file.
  27.     file_num = FreeFile
  28.     Open file_name For Binary As #file_num
  29.  
  30.     ' Copy the data into the file.
  31.     file_length = rs!FileLength
  32.     num_blocks = file_length / BLOCK_SIZE
  33.     left_over = file_length Mod BLOCK_SIZE
  34.  
  35.     For block_num = 1 To num_blocks
  36.         bytes() = rs!Picture.GetChunk(BLOCK_SIZE)
  37.         Put #file_num, , bytes()
  38.     Next block_num
  39.  
  40.     If left_over > 0 Then
  41.         bytes() = rs!Picture.GetChunk(left_over)
  42.         Put #file_num, , bytes()
  43.     End If
  44.  
  45.     Close #file_num
  46.  
  47.     ' Display the picture file.
  48.     picPerson.Picture = LoadPicture(file_name)
  49.     picPerson.Visible = True
  50.  
  51.     Width = picPerson.Left + picPerson.Width + Width - _
  52.         ScaleWidth
  53.     hgt = picPerson.Top + picPerson.Height + Height - _
  54.         ScaleHeight
  55.     If hgt < 1440 Then hgt = 1440
  56.     Height = hgt
  57.  
  58.     Kill file_name
  59.     Screen.MousePointer = vbDefault
  60. End Sub
  61.  
  62. ' Return a temporary file name.
  63. Private Function TemporaryFileName() As String
  64. Dim temp_path As String
  65. Dim temp_file As String
  66. Dim length As Long
  67.  
  68.     ' Get the temporary file path.
  69.     temp_path = Space$(MAX_PATH)
  70.     length = GetTempPath(MAX_PATH, temp_path)
  71.     temp_path = Left$(temp_path, length)
  72.  
  73.     ' Get the file name.
  74.     temp_file = Space$(MAX_PATH)
  75.     GetTempFileName temp_path, "per", 0, temp_file
  76.     TemporaryFileName = Left$(temp_file, InStr(temp_file, _
  77.         Chr$(0)) - 1)
  78. End Function
Oct 8 '06 #2
vinci
62
thanks a lot! it will really help me a lot...
salamat!!! - slapshock
Nov 2 '06 #3
help me guys how to upload photo in vb6..please i nid in my thesis if you have a sample program please send to my email here my email mail id removed thanks to all..godbless
Feb 16 '09 #4
debasisdas
8,127 Expert 4TB
@Rowelle
have you tried the code posted in this thread ?
Feb 16 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: jmev7 | last post by:
Is there a script that will allow upload of multiple images at once to a FTP server, and simultaneously write that list to a MySQL table? I need to allow users to do something like this. Thanks.
3
by: Atz | last post by:
Hi to all ! This is the working, completed PHP script for file upload. The only problem is: wenn i send file and when the file is upload on the server, the file name ( orginal file name is...
10
by: matt | last post by:
I have this code, works perfectly on Windows server, but now i'm trying to run it on a Linux server, the form submits, i get no errors, but the photo doesnt upload, and the caption file doesnt...
5
by: bob garbados | last post by:
I am trying to create a database-driven photo gallery for a friend with an admin form to upload images... I can upload a file to the web server, but I want to store the image in a database and I...
2
by: Nicole Legroe | last post by:
I have an asp.NET site running. I want to make it possible in my webpage, to enter the name of an imagefile on my computer and to upload this file on my webserver. What is the code to make this...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
24
by: owz2008 | last post by:
This has probably been covered before but could not find a similar thread. Basically I have created a form which can be viewed at www.icomworks.co.uk/canvaspayform.html I want to submit the...
12
by: GuangXiN | last post by:
I want the file upload element disappear, instead of it, I place a text box and a button with my own css defination. but it doesn't work on IE7. What should I do now? <form action="upload.php"...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.