473,545 Members | 2,820 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to upload photo in visual basic

slapshock
57 New Member
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 13732
Hemant Pathak
92 Recognized Expert New Member
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 TemporaryFileNa me 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 New Member
thanks a lot! it will really help me a lot...
salamat!!! - slapshock
Nov 2 '06 #3
Rowelle
1 New Member
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 Recognized Expert Expert
@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
10943
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
2500
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 bla.gif ) is converted to something like this img34b123.gif. The file is stored in wright directory and there is no problems with upload. Could this...
10
2856
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 write.... any ideas why?? <?php include 'gall_settings.inc';
5
3239
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 want to resize the image before I save it... How do I take the uploaded .jpg and shrink it to a thumbnail? How do I pass the uploaded .jpg to a stored...
2
1173
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 upload possible? Nicole Legroe nicole.legroe@telenet.be
15
5311
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 path of the uploaded image, and resize it with the provided dimensions. My function is below. The current function is returning an error when run...
21
34349
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 obvious of which is the sharing of files. For example, you upload images to a server to share them with other people over the Internet. Perl comes ready...
24
7684
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 form along with the file so that they are both placed on my server... I have created a folder on my server in my public_html called myscripts and have...
12
2469
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" method="POST" enctype="multipart/form-data"> <p> <span>Select photo:</span> <input type="file" name="photo" style="display:none;"> <input type="text"...
0
7502
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...
0
7434
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...
0
7692
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7946
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7457
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...
0
6026
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...
0
3491
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...
1
1045
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
744
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...

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.