Author Topic: Downloading an Image  (Read 1974 times)

anybody know how to download an image and overwrite an existing one?

What
Best topic today

*has got idea what you are meaning to say

What
Best topic today

*has got idea what you are meaning to say
the hell are you talking about?

phflack look this thing over: http://forum.blockland.us/index.php?topic=204226.0
it doesn't do exactly what you need but you can probably adapt it

thanks
i was making that, just for the loading screen instead, and didn't know that existed

Code: [Select]
function FileDownloader::DownloadFile(%this, %addr, %file, %saveto)
{
%this.addr = %addr;
%this.file = %file;
%this.saveTo = %saveto;
%this.setBinary(0);
%this.connect(%addr @ ":80");
}

function FileDownloader::onConnected(%this)
{
%this.send("POST " @ %this.file @ " HTTP/1.1\r\nHost: "@ %this.addr @"\r\nUser-Agent: Torque/1.0\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 0\r\n\r\n");
}

function FileDownloader::onLine(%this, %line)
{
if(%line $= "" && %this.binSize)
%this.setBinarySize(%this.binSize);
if(getWord(%line,0) $= "Content-Length:")
%this.binSize = getWord(%line,1);
}

function FileDownloader::onBinChunk(%this)
{
cancel(%this.saveSched);
%this.saveSched = %this.schedule(1000, onSaveFinal, %this.saveto);
}

function FileDownloader::onSaveFinal(%this, %saveto)
{
%this.saveBufferToFile(%saveto);
echo("Done saving");
}

Code: [Select]
new tcpObject(FileDownloader);
FileDownloader.downloadFile("google.com","/images/srpr/logo3w.png","config/client/logo.png");

Anyone that wasn't looking for that exact use can use this code to download all other images. That one's specially formulated to download the weird ass format Badspot uses for those pictures and won't work with normal images.
« Last Edit: August 30, 2012, 11:23:25 PM by TripNick »

hmm, it just shows up white ingame and can't be viewed when i try to open the file outside of BL

why is it using POST instead of GET ???
I'm not too clear on which does what myself, but I imagine that GET is probably more correct to use in this instance.

And I don't think that just waiting one second is the proper way to know when a file is done downloading.  Usually you use the chunks in some way to figure out how much is left.

And what if Content-Length: is part of the file you are downloading?  That would break with the online part.
« Last Edit: August 30, 2012, 09:15:39 PM by Nexus »

use Get, and don't include all the stuff headers that you don't need like user-agent, content-type, and content-length.

so, it always shows up with either the no image image that badspot has, or completely white (from unable to be opened)
even when there's an image, it shows up white in BL anyway
not sure what's going on, because opening BL, joining a server and drawing something manualy and then using pop/push updates the background
Code: [Select]
package ServerPreview
{
function ConnectToServer(%a, %b, %c, %d)
{
echo("triggered");
%value = parent::ConnectToServer(%a, %b, %c, %d);
%ip = getSubStr(%a,0,strPos(%a,":"));
%port = getSubStr(%a,strPos(%a,":")+1,strLen(%a));
%address = strreplace(%ip,".","-") @ "_" @ %port;
new tcpObject(FileDownloader);
FileDownloader.downloadFile("image.blockland.us", "/detail.php?q=" @ %address, "base/client/ui/loadingBG.png");
echo("ended");
return %value;
}
};
activatePackage(ServerPreview);

function FileDownloader::DownloadFile(%this, %addr, %file, %saveto)
{
%this.addr = %addr;
%this.file = %file;
%this.saveTo = %saveto;
%this.connect(%addr @ ":80");
}

function FileDownloader::onConnected(%this)
{
%this.send("GET " @ %this.file @ " HTTP/1.1\r\nHost: "@ %this.addr @"\r\n\r\n");
}

function FileDownloader::onLine(%this, %line)
{
if(%line $= "" && %this.binSize)
%this.setBinarySize(%this.binSize);
if(getWord(%line,0) $= "Content-Length:")
%this.binSize = getWord(%line,1);
}

function FileDownloader::onBinChunk(%this)
{
cancel(%this.saveSched);
%this.saveSched = %this.schedule(1000, onSaveFinal, %this.saveto);
}

function FileDownloader::onSaveFinal(%this, %saveto)
{
%this.saveBufferToFile(%saveto);
echo("done loading");
schedule(500, 0, resetLoading);
}

function resetLoading()
{
canvas.popDialog(LoadingGui);
canvas.pushDialog(LoadingGui);
}
« Last Edit: August 30, 2012, 10:19:18 PM by phflack »

why is it using POST instead of GET ???
I'm not too clear on which does what myself, but I imagine that GET is probably more correct to use in this instance.
It's using POST because frankly there's no real reason not to. I adapted it from a script where it would POST variables so I never changed it, and there's really no reason to.

And I don't think that just waiting one second is the proper way to know when a file is done downloading.  Usually you use the chunks in some way to figure out how much is left.
It is if you don't know the length, not all files give you the length prior to downloading them. We discussed this in another topic, you can look it up if you really desire. It was by Plornt.

And what if Content-Length: is part of the file you are downloading?  That would break with the online part.
No it wouldn't, because after the Content-Length: header it switches to binary processing and it no longer calls TCPObject::onLine

use Get, and don't include all the stuff headers that you don't need like user-agent, content-type, and content-length.
I included the headers that I felt were necessary to support all webservers. Content-Length is important to include with the POST header because the webserver may wait for the header or data before serving the request, content-type only helps the webserver support your request, and user-agent makes it so if you're downloading off your own server you can modify the data based on if you're downloading through Torque so that other people can't download the images. I don't write stuff in my code with no reasoning.

so, it always shows up with either the no image image that badspot has, or completely white (from unable to be opened)
even when there's an image, it shows up white in BL anyway
not sure what's going on, because opening BL, joining a server and drawing something manualy and then using pop/push updates the background
I already mentioned before that this code won't work with Badspot's code, though you can make it do that. Change these lines:

if(%line $= "" && %this.binSize) %this.setBinarySize(%this.binSize);
to
if(%line+1-1 $= %line) %this.setBinary(1);
« Last Edit: August 30, 2012, 11:33:01 PM by TripNick »

hmm, i changed that and it makes an unreadable file

hmm, i changed that and it makes an unreadable file
Open it with notepad and paste the first line here.

when i joined the first server, it didn't modify it at all, despite triggering the first two echos
the 2nd server i joined was badspots, which emptied the file (it had two lines of emptyness though?)

when i joined the first server, it didn't modify it at all, despite triggering the first two echos
the 2nd server i joined was badspots, which emptied the file (it had two lines of emptyness though?)
No matter how I change it, I keep getting a bad request error. I'm too tired to really fix up the code to work with chunked downloading (why would you even do that badspot, why) so you should just use Plornt's code which should look oddly familiar (hint: it's derived from mine)

ok, i'll go back to modifying his then