I have a web site that contains some pictures in JPG format.
Is it possible to auto-download the pictures on the this web site using VB codes? Please give some tips. Thanks!
Printable View
I have a web site that contains some pictures in JPG format.
Is it possible to auto-download the pictures on the this web site using VB codes? Please give some tips. Thanks!
Using MSInet:
Code:Dim localFile As String
Dim RemoteFile As String
Dim fnum As Long
RemoteFile = "http://www.site.com/images/image.jpg"
localFile = "c:\image.jpg"
Dim b() As Byte
Dim strURL As String
' Retrieve the file as a byte array.
b() = Inet1.OpenURL(RemoteFile, icByteArray)
fnum = FreeFile
Open localFile For Binary Access _
Write As fnum
Put fnum, , b()
Close fnum
What if I don't know the file names? Is there a way to find out the file names and auto download?
What do you mean? When it is placed on your computer, it's given the name. So you just name it to what you want.
I don't think it's possible to start downloading files from a site unless you state what to download. Of course, you could always use MSInet for FTP and just download the whole list of files located on the site.
Matthews,
How do you use MSInet for FTP to down load the whole list of files? Please post some code... Thanks!
Here is an example of how to make an FTP using Inet: InetFTP
And to download the whole list, you'll have to make your own, because that's not included. Probably something like:
Code:On Error Resume Next
For i = 0 to List1.ListCount
Call CmdDownload_Click 'download button
List1.ListIndex = List1.ListIndex + 1
DoEvents
Next i
what if I don't have the file names? Is there a way to get all the file names of the sites and down load them? Thanks!
FTP will list all the files contained on the site. Than you save them as what they are on the list.
I believe the link to the FTP I gave you will do that.
yuo could probably check with inet, and search the server for all .jpg extensions, then download them
If the files are on a web server and you want to get the whole list you will likely have trouble. The only time you can view the list is when you request a valid path (like http://www.microsoft.com/office/ AND the web server has no default file for the path (index or default .htm, .html, .asp or whatever) AND the web server has directory browsing turned on. then you will get a file list and you can set an array of inet controls to download them.
Directory browsing is usually turned OFF (probably for just this reason :D)
OR
if one of the html files has the images on it (or links to it) then you can read the html page and into a list and get the images there.
If you're using FTP the ls command will give you a list of files in that directory
Applies To
Microsoft Internet Transfer Control
Executes a request to a remote server. You can only send requests which are valid for the particular protocol.
Syntax
object.Execute url, operation, data, requestHeaders
The Execute property syntax has these parts:
Part Description
object An object expression that evaluates to an object in the Applies To list.
url Optional. String that specifies the URL to which the control should connect. If no URL is specified here, the URL specified in the URL property will be used.
operation Optional. String that specifies the type of operation to be executed. See Settings below for a list of supported operations.
data Optional. String that specifies the data for operations (See Settings below.)
requestHeaders Optional. String that specifies additional headers to be sent from the remote server. The format for these is:header name: header value vbCrLf
Settings
Note Valid settings for operation are determined by the protocol being used. The tables below are organized by protocol.
Supported HTTP commands
Valid settings for operation are:
Operation Description
GET Retrieve data from the URL specified in the URL property.
HEAD Sends the Request headers.
POST Posts data to the server. The data is located in the data argument. This is an alternate method to GET, for which additional instructions are specified in the data argument.
PUT Put operation. The name of the page to be replaced is located in the data argument.
Supported FTP commands
Important The FTP protocol uses a single string that includes the operation name and any other parameters needed by the operation. In other words, the data and requestHeaders arguments are not used; all of the operations and their parameters are passed as a single string in the operation argument. Parameters are separated by a space. In the descriptions below, do not confuse the terms "file1" and "file2” with the data and requestHeaders arguments.
The syntax for FTP operations is:
operationName file1 file2.
For example, to get a file, the following code invokes the Execute method, which includes the operation name ("GET"), and the two file names required by the operation:
Inet1.Execute "FTP://ftp.microsoft.com", _
"GET Disclaimer.txt C:\Temp\Disclaimer.txt"
Note File names that include embedded spaces are not supported.
..
Valid FTP settings for operation are:
Operation Description
CD file1 Change Directory. Changes to the directory specified in file1.
CDUP Change to parent directory. Equivalent to "CD.."
CLOSE Closes the current FTP connection.
DELETE file1 Deletes the file specified in file1.
DIR file1 Directory. Searches the directory specified in file1. (Wildcards are permitted but the remote host dictates the syntax.) If no file1 is specified, a full directory of the current working directory is returned.Use the GetChunk method to return the directory data.
GET file1 file2 Retrieves the remote file specified in file1, and creates a new local file specified in file2.
LS file1 List. Searches the directory specified in file1. (Wildcards are permitted but the remote host dictates the syntax.) Use the GetChunk method to return the file directory data.
MKDIR file1 Make Directory. Creates a directory as specified in file1. Success is dependent on user privileges on the remote host.
PUT file1 file2 Copies a local file specified in file1 to the remote host specified in file2.
PWD Print Working Directory. Returns the current directory name. Use the GetChunk method to return the data.
QUIT Terminates the current user.
RECV file1 file2 Retrieves the remote file specified in file1, and creates a new local file specified in file2. Equivalent to GET.
RENAME file1 file2 Renames the remote file named in file1 to the new name specified in file2. Success is dependent on user privileges on the remote host.
RMDIR file1 Remove Directory. Removes the remote directory specified in file1. Success is dependent on user privileges on the remote host.
SEND file1 file2 Copies a local file, specified in file1, to the remote host, specified in file2. Equivalent to PUT.
SIZE file1 Returns the size of the directory specified in file1.
Return Type
None
Remarks
Many commands listed above can be carried out only if the user has privileges on the host server. For example, anonymous FTP sites will not allow anyone to delete files or directories.
You want to use the LS operation with *.jpg or whatever image type you want. And using the GetChunk operation will feed you all the files that are jpg's then you can Get them however you want.
yeah I mentioned the LS command... but you can't ftp to a web server!
paul282, How do you read the HTML page with VB? Thanks!
laura, using MSInet control:
But that will not always work. It's better to use Webbrowser to get the source. Sometimes MSInet will timeout or not get the whole page, while the Webbrowser Control will always get all of it.Code:Text1.text = Inet1.OpenURL("http://www.site.com")
Code:Text1.Text = Webbrowser1.Document.documentElement.innerHTML
Matthew, I'm new to the webbrowser control. How do you use the webbrowser? Please post some code. Thanks!
Here are the basics:
That's basically all you need to make a Webbrowser.Code:'Go to the previous webpage
WebBrowser1.GoBack
'Go to the forward webpage
WebBrowser1.GoForward
'Go to the default IE home
WebBrowser1.GoHome
'Go to the default search page
WebBrowser1.GoSearch
'Refresh the current webpage
WebBrowser1.Refresh
'Navigate to a webpage
WebBrowser1.Navigate "www.YourSite.com"
Matthew, I tried the code:
Text1.Text = Webbrowser1.Document.documentElement.innerHTML
but I get an error '91'-object variable or with block variable not set. I do have a control on the form and a text box. Any ideas? Thanks!
You say you put the Webbrowser Control on the form. I am not sure then. But I sent you an example of how to do it.
Hope that helps.
Why do I get an error when I try this?
Text1.text = Webbrowser1.Document.documentElement.innerHTML
I do have Webbrowser1 on the form and a text box. Does anyone familiar with this?
Did you navigate to a site before getting the source?
Put a textbox, webbrowser control and a command button in your program and then use this code:That should do the trick. :DCode:Option Explicit
Private Sub Command1_Click()
WebBrowser1.Navigate2 "www.yahoo.com"
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If pDisp Is WebBrowser1.Object Then
Text1.Text = WebBrowser1.Document.documentElement.innerHTML
End If
End Sub
All the best.
Dear all,
I tried the suggestion from OneSource and it works. I think the problem was I try to get the source page before it finish loading it or something like that. Thanks everyone for helping me out...
i think the inet way is the easiest, but, the choice is yours.
really, i was unaware of thatQuote:
Originally posted by Matthew Gates
Yeah, but MSInet will timeout sometimes and not get the whole page. So you will be left with half of the html source, while if you were to use the Webbrowser control, you could get all of the source always. Of course, you'd always have to navigate to the site too, but if you want the whole source, than it's worth it.Quote:
Originally posted by da_silvy
i think the inet way is the easiest, but, the choice is yours.
oh well,
you learn something new every day!