[MAXGUI] Download files over internet?

BlitzMax Forums/BlitzMax Beginners Area/[MAXGUI] Download files over internet?

Morning all.

Are there any newbie friendly functions / modules to download/upload files over the internet?

My app needs to download ogg files and textdata for updates.
Has not to be extremely fast! (Win32/Mac/Linux if possible)

Grisu

Ftp or Http?

http and download only will do for now.

I don't know how I would be able to upload stuff anyway = Newbie.

bank:TBank = LoadBank("http::www.google.co.uk/intl/en_uk/images/logo.gif")
SaveBank(bank, "c:/google logo.gif")


I'd imagine uploading could be done by sending data via a stream to your server side app (php script, compiled executable or whatever) and you then write it to disk. You just need to be sure that it's hacker safe.

(i.e. don't allow people to upload a cgi-script of their choice that they can then run on your server). Uploading to a directory that has no execute permissions would be a good start.

@ Ian:
Nice and simple.
Can I display "downloadspeed", "time left", etc?

@ Perturbatio: Thanks for info. Think I will leave that aside for the moment. Too risky.

Can I display "downloadspeed", "time left", etc?


Not with the built-in commands -- since they are not threaded, the application itself will not be able to do anything else while it is downloading the file, which includes printing anything to the screen.

Possibly with streams?

So basicly the whole app "hangs" if I download bigger files.
And I can't show a progress bar and such "normal" informations... :/

*puts multi-threadding on his long bmx whishlist...

You can use streams and/or sockets to do this, there is an example in the docs under ReadStream showing how to read a webpage line by line.

Sorry Grisu, I missed this earlier.

As Azathoth has said, you can do this with a TCP stream. I'm a bit tied up at the moment, but I'll post an example tomorrow.

[edit]
Okay, I sorted things quicker than expected.
It's not pretty, but it should give you the general idea.
Strict

Print HTTPGetFile("http://www.google.co.uk/intl/en_uk/images/logo.gif", "c:\google logo.gif")

End


Function HTTPGetFile(url$, filePath$)
	url$ = url$.Replace("http://", "")
	Local slashPos = url$.Find("/"), host$, file$
	
	If slashPos <> -1
		host$ = url$[..slashPos]
		file$ = url$[slashPos..]
	Else
		Return -1
	EndIf
	
	Local fileStream:TStream = OpenStream(filePath$, False, True)
	If Not(fileStream) Then Return -1
	
	Local TCPStream:TStream = OpenStream("tcp::" + host$)
	If Not(TCPStream)
		fileStream.Close()
		Return -1
	EndIf
	
	TCPStream.WriteLine "GET " + file$ + " HTTP/1.1"
	TCPStream.WriteLine "Host: " + host$
	TCPStream.WriteLine "Connection: close"
	TCPStream.WriteLine ""
	
	Local dataLength
	Local in$ = TCPStream.ReadLine()
	
	If in$.Find("200") < 0
		TCPStream.Close()
		fileStream.Close()
		Return -1
	EndIf
	
	While in$
		If in$.Find("Content-Length:") <> -1
			dataLength = Int(in$[in$.Find(":") + 1..].Trim())
		EndIf
		
		in$ = TCPStream.ReadLine()
	Wend
	
	Local byteCount
	While Not(TCPStream.Eof())
		'You'd probaly want to download more than a single byte at a time here
		fileStream.WriteByte(TCPStream.ReadByte())
		
		'You'd normaly update a progress bar here...
		Print "Done " + ((byteCount / Float(dataLength - 1)) * 100) + "%"
		
		'add number of bytes downloaded
		byteCount :+ 1
	Wend
	
	TCPStream.Close()
	fileStream.Close()
	
	If byteCount <> dataLength Then Return -1
End Function

[/edit]

Yepp, it does!
I think I can take over here.. :)
Thanks!