Promotic
WikipediaLinkedInYoutubeTwitterFacebook

How to save a binary file (image) from url address

The following script allows to download and save a binary file from specific url address. The Microsoft HTTP request technology is used in this script in order to download the binary file from url address. The Ado technology is used for saving the contents of the file to disk. Both technologies are launched by the CreateObject method.
An example of download and save of image generated by web camera:
The script can be included into the application for example into the objects PmTimer, PmKey, ...
Dim oHttp, sUrl, sSavePath, oStream
  
sSavePath = Pm.DiscGetPath("#appres:") & "MyPicture.jpg"
sUrl = "http://www.harrachtour.cz/img_cam.jpg"
  
Set oHttp = CreateObject("Microsoft.XMLHTTP")
  
oHttp.Open "GET", sUrl, false ', "username", "password" 'username and password only if proxy server
oHttp.setRequestHeader "Content-Type", "image/multipart"
oHttp.send
If oHttp.readyState = 4 And oHttp.status = 200 Then
    Set oStream = CreateObject("adodb.stream")
    oStream.type = 1 '1=adTypeBinary
    oStream.open
    oStream.write oHttp.responseBody
    oStream.savetofile sSavePath, 2 '2=adSaveCreateOverWrite, 1=adSaveCreateNotExist
    Set oStream = nothing
End If
oHttp.abort
Set oHttp = nothing
© MICROSYS, spol. s r. o.Tavičská 845/21 703 00 Ostrava-Vítkovice