Download Image from website (1 Viewer)

mlh407

Registered User.
Local time
Yesterday, 20:58
Joined
Jun 24, 2005
Messages
26
Hi Everyone,
Is there a way for me, using access, to press a button and download an image from a website. Then display that image inside an access form?

Thanks,
Michael
 

ByteMyzer

AWF VIP
Local time
Yesterday, 20:58
Joined
May 3, 2004
Messages
1,402
There is. One method uses the ADODB.Stream object and the xmlHTTP object to retrieve the file from the web and to save it to your computer.

Create a new Module, paste the following text into it and save the module as modHtthGet:
Code:
Option Compare Database
Option Explicit

Const adTypeBinary = 1
Const adSaveCreateOverwrite = 2
Const adModeReadWrite = 3

Public Sub httpGet(sourcePath As String, destinationPath As String)

    Dim xmlHTTP
    Dim contents
    Dim oStr

    Set xmlHTTP = CreateObject("Microsoft.XMLHTTP")
    xmlHTTP.Open "GET", sourcePath, False
    xmlHTTP.send
    contents = xmlHTTP.responseBody

    Set oStr = CreateObject("ADODB.Stream")
    oStr.Mode = adModeReadWrite
    oStr.Type = adTypeBinary
    oStr.Open

    oStr.Write (contents)
    oStr.SaveToFile destinationPath, adSaveCreateOverwrite

End Sub

You can call this sub with something like:
Code:
httpGet "http://i2.microsoft.com/h/all/i/ms_masthead_8x6a_ltr.jpg", _
"C:\ms_masthead_8x6a_ltr.jpg"

See if this works for you.
 

nhtuan

New member
Local time
Yesterday, 20:58
Joined
Dec 23, 2010
Messages
24
There is. One method uses the ADODB.Stream object and the xmlHTTP object to retrieve the file from the web and to save it to your computer.

Create a new Module, paste the following text into it and save the module as modHtthGet:
Code:
Option Compare Database
Option Explicit

Const adTypeBinary = 1
Const adSaveCreateOverwrite = 2
Const adModeReadWrite = 3

Public Sub httpGet(sourcePath As String, destinationPath As String)

    Dim xmlHTTP
    Dim contents
    Dim oStr

    Set xmlHTTP = CreateObject("Microsoft.XMLHTTP")
    xmlHTTP.Open "GET", sourcePath, False
    xmlHTTP.send
    contents = xmlHTTP.responseBody

    Set oStr = CreateObject("ADODB.Stream")
    oStr.Mode = adModeReadWrite
    oStr.Type = adTypeBinary
    oStr.Open

    oStr.Write (contents)
    oStr.SaveToFile destinationPath, adSaveCreateOverwrite

End Sub

You can call this sub with something like:
Code:
httpGet "http://i2.microsoft.com/h/all/i/ms_masthead_8x6a_ltr.jpg", _
"C:\ms_masthead_8x6a_ltr.jpg"

See if this works for you.
It works perfectly, many thanks.
 

Richard Horne

Member
Local time
Today, 04:58
Joined
Oct 15, 2020
Messages
51
Hi ByteMyzer

I've been using this excellent script of yours and it works very well for the most part, apart from one small issue.

Let's say we have image1.jpg on a web server and we capture that image using your script, save it locally then load it on a form in an image control. If image1.jpg is updated outside of Access, i.e. through FTP, when repeating the httpGet call, Access will still show the original image and not the newly-uploaded image.

It's as though there's a cached version of that image somewhere and Access is loading that instead of the new file.

The issue also exists when viewing the image in a browser, but of course you can CTRL+F5 to force a refresh. Is there some way to do the same with a locally cached file?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 11:58
Joined
May 7, 2009
Messages
16,812
you requery your Image control by Setting again the Picture property to the Local path and filename of the image file.
 

Richard Horne

Member
Local time
Today, 04:58
Joined
Oct 15, 2020
Messages
51
you requery your Image control by Setting again the Picture property to the Local path and filename of the image file.
That's exactly what I'm already doing but it doesn't seem to refresh the image and still loads the old version from somewhere.

Code:
'Get the URL of where the image would be if it's saved
Dim str_url As String
str_url = "https://www.MYURL.COM/catalogue/" & Me.cbo_product_code & ".jpg"

'Check if the file exists
If CheckNetFile(str_url) = True Then

    'If it does exist on the online server then save a local copy
    httpGet "https://www.MYURL.COM/catalogue/" & Me.cbo_product_code & ".jpg", "C:\DEAN\Catalogue\" & Me.cbo_product_code & ".jpg"
    
    'Then load the downloaded local copy
    Me.img_product.Picture = "C:\DEAN\Catalogue\" & Me.cbo_product_code & ".jpg"
Else
    'Display no image available
    Me.img_product.Picture = "C:\DEAN\Catalogue\no_image_available.jpg"
End If

I've also even tried deleting the local image first before redownloading it but that doesn't fix it either.

Code:
'Delete the local file first
'Kill "C:\DEAN\Catalogue\" & Me.cbo_product_code & ".jpg"
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 11:58
Joined
May 7, 2009
Messages
16,812
maybe there is no New version?
also the httpGet function doesn't have a way to check if the image
was successfully fetched before proceeding with the rest of code, like:

...
...
xmlHTTP.send
while xmlHTTP.ReadyState <> 4
DoEvents
wend
contents = xmlHTTP.responseBody
 

Richard Horne

Member
Local time
Today, 04:58
Joined
Oct 15, 2020
Messages
51
There is a new version, I'm uploading one speficially to test for this issue as my users are reporting it. My tests are all failing. Going directly to the URL shows the new version. Going directly to the locally-saved version shows the old version.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 20:58
Joined
Oct 29, 2018
Messages
19,167
There is a new version, I'm uploading one speficially to test for this issue as my users are reporting it. My tests are all failing. Going directly to the URL shows the new version. Going directly to the locally-saved version shows the old version.
I imagine this could suffer from the same problem; but since I am currently not in front of a computer, would you mind testing it for me?

 

Richard Horne

Member
Local time
Today, 04:58
Joined
Oct 15, 2020
Messages
51
Thanks for the update theDBguy. I tried your script and still have the same problem I'm afraid.

One thing I haven't mentioned is that my Access Database is running through Microsoft Remote Desktop on an Azure Cloud server. I don't see that this should make any difference to be honest, but it's all useful context.

What's weird, however, is I have just logged into the VM and the image I was testing has reverted back to the previous version and my updated version is no more. Something really strange is going on here.

Ideally I'd like to just be able to view images from the web in my forms without having to download them (this would solve this whole issue). So far I've had no joy researching this, but is it possible to view an image from the web on a form instead? This would be a much better solution.
 

Richard Horne

Member
Local time
Today, 04:58
Joined
Oct 15, 2020
Messages
51
No I haven't, but I will investigate that now. Didn't even know it was an option.
 

Richard Horne

Member
Local time
Today, 04:58
Joined
Oct 15, 2020
Messages
51
It's not there a under the list of controls in form design. I'm using Access 365 but my DB is an MDB - is this limiting my options?
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 20:58
Joined
Oct 29, 2018
Messages
19,167
It's not there a under the list of controls in form design. I'm using Access 365 but my DB is an MDB - is this limiting my options?
Hmm, I don't have a mdb to try, so I'm not sure about that.
 

Users who are viewing this thread

Top Bottom