hello..i got some question about downloding and save the image from other server to my server and also change the file name.
i have no idea on it. i hope can get some help from here.

let say i have a url that pass back from the Picnik API like this:

i would like to get this image physical files and save it into my server, and replace it to my original image.

how can i do it? using aspjepg? or aspsmart upload?

thanx..hope to hear from u all soon...
thank you very very much

Dani AI

Generated

You do not need a browser or upload control for this; in Classic ASP you can fetch the bytes over HTTP and stream them to disk, then swap the file in place. Do make sure you have permission to store a copy of the asset, as rightly flagged. Also, , you do not need a “virtual browser” here; a server-side HTTP client is enough. The snippet below hardens ’s idea by using ServerXMLHTTP (better for server code), checking Content-Type, mapping the right extension, and replacing the target atomically.

<%
Function SaveRemoteImage(url, saveDir, baseName)
  Dim http: Set http = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
  http.setTimeouts 5000,5000,15000,15000
  http.open "GET", url, False
  http.setRequestHeader "User-Agent","ClassicASP-Downloader/1.0"
  http.send
  If http.status <> 200 Then Err.Raise vbObjectError+1,"","HTTP " & http.status

  Dim ctype: ctype = LCase(http.getResponseHeader("Content-Type"))
  If Left(ctype,6) <> "image/" Then Err.Raise vbObjectError+2,"","Not an image"

  Dim ext: ext = ChooseExt(ctype, url)
  Dim fso: Set fso = Server.CreateObject("Scripting.FileSystemObject")
  Dim tmp: tmp = saveDir & "\_" & baseName & "." & ext & ".tmp"
  Dim out: out = saveDir & "\" & baseName & "." & ext

  Dim s: Set s = Server.CreateObject("ADODB.Stream")
  s.Type = 1: s.Open: s.Write http.responseBody
  s.SaveToFile tmp, 2: s.Close
  If fso.FileExists(out) Then fso.DeleteFile out, True
  fso.MoveFile tmp, out
End Function

Function ChooseExt(ctype, url)
  Select Case ctype
    Case "image/jpeg": ChooseExt="jpg"
    Case "image/png":  ChooseExt="png"
    Case "image/gif":  ChooseExt="gif"
    Case Else: ChooseExt = LCase(Split(Split(url,"?")(0),".")(UBound(Split(Split(url,"?")(0),"."))))
  End Select
End Function
%>

Tips:

  • Prefer ServerXMLHTTP over XMLHTTP for server code; set timeouts and handle non-200 responses.
  • Do not trust the URL’s extension; use Content-Type first.
  • Write to a temp file, then move it to the final name to avoid half-written files when replacing the original.
  • If the API requires headers (Referer, Cookie, Authorization), add them with http.setRequestHeader before send.
  • If you are on ASP.NET instead, ’s approach applies; for Classic ASP, stick with the pattern above.

Recommended Answers

All 9 Replies

right click on the image... save as... then upload it to your server or photobucket or imageshack.....

In photobucket you can put the URL in and then it uploads that photo to your account.

Done :)

Aren't you forgetting something cohen? What about copyrights?

Aren't you forgetting something cohen? What about copyrights?

oh.... and there's that..... didn't think about that....

for yenyen - you would need to check that with the domain webmaster.

Hi,
aside the mentioned restrictions, saving images on your server would be quite difficult. Usually the images are sent to the browser, that means you have to use a virtual browser in order to get the image onto your server. A browser receives all images, in this case your server would receive many images from the other server.

You guys sure give up easy :)

I wrote this little script for you guys, examples included.

<%
'Fetch and save this image
Call fetchDocument("", Server.MapPath("./"))

'Fetech and save this document
Call fetchDocument("http://www.daniweb.com/forums/thread153966.html", Server.MapPath("./"))


'Created by Drew Gauderman
'Website: [url]
Function fetchDocument(strDocURL, strSavePath)
	'Create the XMLHTTP object to fetch the image
	Set objXmlHTTP = Server.CreateObject("Microsoft.XMLHTTP")

	'Get the image
	objXmlHTTP.Open "GET", strDocURL, False
	objXmlHTTP.Send

	'Create Stream object
	Dim objStream
	Set objStream = Server.CreateObject("ADODB.Stream")

	With objStream
		'Specify stream type - we want To save binary data.
		.Type = 1

		'Open the stream And write binary data To the object
		.Open
		.Write objXmlHTTP.ResponseBody

		'Save binary data To disk
		.SaveToFile strSavePath & "/" & Mid(strDocURL, InstrRev(strDocURL, "/")), 2

		'Clean up objects
		.Close
	End WIth

	Set objXmlHTTP = Nothing
	Set objStream = Nothing
End Function
%>

Drew,

You rock! This is just what I was looking for... I was searching for a way to save webpage thumbnails created from websnapr.com for my website: <URL SNIPPED>

I could have used websnapr to dynamically create the thumbnails each time the page was loaded, but figured if I could save them it would be better/easier to controll.... also, by saving the images I can include them in my RSS feed (websnapr images dont work with RSS).

Anyhow, thanks Drew for your awesome and simple example... here's teh code I used:

<%
'Fetch and save this image
'Call fetchDocument("", Server.MapPath("./"), "myimagename.jpg")

'Fetech and save this document
'Call fetchDocument("http://www.daniweb.com/forums/thread153966.html", Server.MapPath("./"), "mydocumentname.doc"))


'Created by Drew Gauderman
'Website: [url]
Function fetchDocument(strDocURL, strSavePath, newImageName)
	'Create the XMLHTTP object to fetch the image
	Set objXmlHTTP = Server.CreateObject("Microsoft.XMLHTTP")

	'Get the image
	objXmlHTTP.Open "GET", strDocURL, False
	objXmlHTTP.Send

	'Create Stream object
	Dim objStream
	Set objStream = Server.CreateObject("ADODB.Stream")

	With objStream
		'Specify stream type - we want To save binary data.
		.Type = 1

		'Open the stream And write binary data To the object
		.Open
		.Write objXmlHTTP.ResponseBody

		'Save binary data To disk
		.SaveToFile strSavePath & "\" & newImageName

		'Clean up objects
		.Close
	End WIth

	Set objXmlHTTP = Nothing
	Set objStream = Nothing
End Function
%>

This way I can save the image to the path I want and also save the filename using a key-identifier with the record I'm saving... making it easy to dynamically create and delete.

Thanks again.

~Mikel

Glad you like it :)

this is a solution how to save image from url to your server:
1. get Stream from URL using WebRequest class
2. Create Image object from stream using System.Drawing.Image.FromStream method
3. Save file on disk using System.Drawing.Image.Save method

details:
http://mxdev.blogspot.com/2008/12/get-image-by-url-net-aspnet.html

or you mean "server" is not the server from which code is executed?

hello..i got some question about downloding and save the image from other server to my server and also change the file name.
i have no idea on it. i hope can get some help from here.

let say i have a url that pass back from the Picnik API like this:

i would like to get this image physical files and save it into my server, and replace it to my original image.

how can i do it? using aspjepg? or aspsmart upload?

thanx..hope to hear from u all soon...
thank you very very much

That is ASP.Net, and that is completely different from ASP. Might want to pay attention next time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.