Downloading files

All I want to do is download a file from the internet, check its hash and save it to a specified location.

I’m a little confused on how to do it. The included Xojo Examples and other example code I’ve seen do way more than what I want so I’m not sure what code I need and what I don’t.

I am creating a TCG game and upon first launch I need the game to download the latest Card Definition file stored in my cloud server. Also this file will be updated as I add more cards to the game.

I figured the best approach is to create a Global “DownloadFile” Function where I pass the web address and save location. This way I can download needed files from anywhere in the game without having duplicate code everywhere.

dim s as string
s= HTTPSocket.Get("www.mywebsite.com/somefolder/thefiletodownload.txt", 30)

s now contains the contents of the file.

I have to store the contents of the file in a variable? There is no way to directly write it to storage. What about non-text based files?

Check the language reference. There’s a form of Get that takes a folderitem and downloads to it. But really, all files are strings. You can read any file that way.

What is the difference between Asynchronous and Synchronous downloading? Which should I use?

While the file is being downloaded, I get the Beach Ball, how do I incorporate a progress bar as I need the file to completely download before executing more code.

If you don’t want your app to stall while downloading, use the async version (no timeout). Put the rest of your code in the completed event. You can use AddHandler to make it more flexible.

I am using the async version but it still stalls

This is what I am using:

httpDownload.Get(strURL, fldrPath)

This is all new to me so I need some example code to learn what to do. I am very visual.

I can’t store the contents of the file in a variable as the file will grow to several hundred MBs so it needs to be saved to storage while downloading.

Also I’ve been out of the loops for several years so some of this stuff is all new to me again so please bear with me

I’m still trying to figure out how to download a file while incorporating a progress bar to show the download progress.

Where do I put the code to download the file? In a thread, timer? I know the progress bar needs a timer to update.

[quote=88271:@Charles Fasano]I’m still trying to figure out how to download a file while incorporating a progress bar to show the download progress.

Where do I put the code to download the file? In a thread, timer? I know the progress bar needs a timer to update.[/quote]

Add httpDownload.Yield = true to prevent the stall.

I do not believe the ProgressBar is possible, as even httpSocket.LookAhead does not seem to work to monitor download.

There is no other way to compare how many bytes have downloaded to the file size? I thought there were other methods that you can call to find out how big a file is before downloading it?

I see there is an HTTPSocket.ReceiveProgress to track the progress of the download. How can I use that with the Progress Bar?

Look at and study LR HTTPSocket.ReceiveProgress http://documentation.xojo.com/index.php/HTTPSocket.ReceiveProgress
Look at and study LR Timer http://documentation.xojo.com/index.php/Timer
Open and study Xojo 2014 Release 1.1/Example Projects/Desktop/Controls/ProgressBar.xojo_binary_project

Everything is there.

I’ve already looked at the HTTPSocket.ReceiveBytes before posting but it doesn’t tell me how to use it. All it says is that it fires periodically but it doesn’t give me a way to see the data.

EDIT: I’m still getting used to the Xojo IDE. I’m used to the old RealBasic IDE that let me click the arrow on an item and see all the available methods. I keep forgetting that I have to manually add an event to a control.

Everything is there :

HTTPSocket.ReceiveProgress ( BytesReceived as Integer, TotalBytes as Integer, NewData as String ) ProgressBar.Maximum = TotalBytes ProgressBar.Value = BytesReceived

You do not even need a timer.

That didn’t quite work. I got a NilObjectException on the:

ProgressBar.Maximum = totalBytes

part.

Is the HTTPSocket.Get supposed to be in a Thread? I used the Yield property and it still hangs.

Did you create a progressbar control?

and no to the thread

Calling Get without a timeout parameter will cause the download to happen asynchronously with the events firing as things are downloaded. You do not need a Thread or a Timer, just a little code in the ReceiveProgress event handler.

Here’s a simple example (to be included in the next release of Xojo) that shows you how you can use an HTTPSocket to download a file and display a ProgressBar.

HttpDownloader Example

[quote=88303:@Charles Fasano]That didn’t quite work. I got a NilObjectException on the:

ProgressBar.Maximum = totalBytes
part.[/quote]

Progressbar.Maximum should be
TheNameOfMyProgressBarInstance.Maximum
(replace “TheNameOfMyProgressBarInstance” with the name of your progressbar instance)
same for Progressbar.Value
:wink:

I know that. I was putting what the object is, not what I am calling it.