How do I programmatically save images from other websites into my webserver?

throwithere
throwithere used Ask the Experts™
on
Say I would like to save an image, ie, from http://www.google.com/images/logo.gif using vbscript and filesystemobject.  How do i go about doing this?
Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
The best possible solution would be to use an activeX object to read in the binary data of an image.

If you have the ability to create/upload, and utilize custom activeX controls on your server, you could create a simple application to save files.

For example, you could use an inet control and pass it the url of the image..



Dim b_byte() As Byte
Dim FileNumber As Integer
b_byte() = inet1.OpenURL(Trim(myURL), icByteArray) 'retrive the byte array

FF = FreeFile
Open txtFileName For Binary Access Write As #FF
Put #FF, , b_byte()
Close #FF


this just shows you how to write binary data of the specified image. If you need more help with creating the aX object itself.. feel free to email me. xmetrix@xmetrix.net or send me an instant message: xmetrix

Commented:
Without an activeX component, you can't easily do this.  I suppose that you could instantiate Internet Explorer, navigate to the file and then check the "Temporary Internet Folders" for a copy of the file, but that's not very elegant.  If you do any vb, its very easy to create an activex dll that can download the file and save it.  Simply use the API call:
Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

Author

Commented:
all i want is to be able to save an archive of the image files from another website like
http://www.site.com/2002/nov/01/image.gif
http://www.site.com/2002/nov/02/image.gif
http://www.site.com/2002/nov/03/image.gif
etc.
into a directory in my webserver as
2002-nov-01.gif
2002-nov-02.gif
2002-nov-03.gif
etc.
problem is reading the binary image so i can save it with a different filename.
any simple solution/s?
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Commented:
does this have to be ran from your site and from within an asp file?

If not, i could tell you how to do it with vb, and or an activeX document.


for example, an adult webmaster came to me and he required a file that would go to tgp's and save all images from the gallery every day.  He would login, hit grab, it would read the images in, and write them to his server's sql db, with option to write to folder/enumerated-file.jpg

if this is something that your trying to accomplish, not necessarily with adult content, but in general the same process, it's fairly simple with an activex control

Author

Commented:
yes, actually that is what i am trying to do.  all i need is to input the source files and i can also specify the output filename.  im trying to rip daily comic strip archive images from a website (unfortunately, they all have the same name but in different directories so i cannot save them easily with enumerated filenames).
Do a search on google for screen scraping and XMLHTTP.

Keywords:
ASP, XMLHTTP, Screen Scraping

- Justin
Commented:
<%

Dim binImage, sUrl, sFilePath
URL = "http://www.experts-exchange.com/v3.0/images/ee-LogoGray.gif"

binImage =     ReadImageFromUrl(sUrl)
sFilePath = "c:\temp\" & Year(date()) & "_" & Month(date()) & "_" & Day(date()) & "_" & Minute(Now()) & "_" & Second(Now()) & ".gif"

WriteBinaryFile    sFilePath, binImage



Function ReadImageFromUrl(url)
    Dim oXMLHttpRequest, sContentType, iContentLength
    Set oXMLHttpRequest = Server.CreateObject("Microsoft.XMLHTTP")
    oXMLHttpRequest.open "GET", url, False
    oXMLHttpRequest.send
    sContentType = oXMLHttpRequest.getResponseHeader("Content-Type")
    iContentLength = oXMLHttpRequest.getResponseHeader("Content-Length")
    Response.write "sContentType = " & sContentType & "<br>"

    ReadImageFromUrl = oXMLHttpRequest.responseBody
    Set oXMLHttpRequest = Nothing
End Function    


Sub WriteBinaryFile(sFilePath, sStream)
    Dim oFSO, oFile, bTest

    Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
    oFSO.CreateTextFile(sFilePath)
    Set oFile = oFSO.OpenTextFile(sFilePath,8)
    oFile.Write(Byte2String(sStream))
    oFile.Close
    Set oFile = Nothing
    Set oFSO = Nothing

End Sub

Function Byte2String(s)
    Dim i
    For i = 1 to LenB(s)
        Byte2String = Byte2String & CHR(AscB(MidB(s,i,1)))
    Next
End Function


%>

Author

Commented:
perfect solution for my problem.
thanx.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial