Download Manager in REALbasic line

In this great tutorial we will learn in few easy steps how to create a Download Manager using REALbasic. We will discover a method to download a file with live progressbar and a option to get the file name and extension from URL. Watch it, learn it, enjoy it!

Creating Download Manager GUI

We will create a very basic GUI for our app so add to Window1 2x StaticText Control (File URL, Status), one TextField Control (for file url location), one Listbox Control (for displaying download status) and one PushButton Control (Download button). When you finish you should get something like this:

Download Manager in REALbasic

HTTPSocket Class in REALbasic

At this moment we will create a class named hget with Super Value HTTPSocket. We will use it to download a file and show the progress of download. If you don’t know how to do this simply go to my article Creating a Class in REALbasic and you will find there all answers you need to go next.

Now inside hget Class go to Event Handlers Sub Connected () we can display info that our download has started. We can do this with one line of code and our Listbox Control.

1
Window1.Listbox1.AddRow "Download Started"

Remaining in hget Class code editor go to Event Handlers Sub DownloadComplete () and enter there:

1
2
3
me.Close //closing the connection when download finish
 
Window1.Listbox1.AddRow "Download Complete" //add info to Listbox

The last thing we will do in hget Class is making visual progress of file download. We do this in Event Handlers Sub ReceiveProgress.

1
2
3
4
5
Window1.ProgressBar1.Maximum = totalbytes
 
Window1.ProgressBar1.Value = bytesReceived
 
Window1.ProgressBar1.Refresh

Get file name and extension from URL

Well how can we do this? The best way to do this is knowing that URL is constructed in such a way that mark / separates the file locations, folders. If we know that we can find the last mark / and then we will copy everything before it (this will be the file name and extension). The easiest method to do it is finding the first mark / from the right of URL.

For example when we use this link:

http://realbasic.tutspolis.com/files/tutorials/downloadmanager.png

we will find this:

/downloadmanager.png

We can find the file name and extension from URL using this method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Dim i, n As Integer
Dim s, a, filename As String
Dim b As Boolean
 
b = true
 
for i = 0 to Len(TextField1.Text) //check all mark in URL - Len returns the number of characters in the specified string
 
  s = Right(TextField1.Text, i) //get mark from right i + 1
 
  a = Mid(s, 1 ,1) //check the firs mark from s Right count
 
  if a = "/" and b = true then //when we find the first mark / we will save the position to n and stop replacing it because in URL are more / marks
 
    b= false
    n = i 
  end
 
next
 
filename = Right(TextField1.Text, n - 1) //returns the file name

Download a file using HTTPSocket

To download a file with displaying the progress in ProgressBar control using HTTPSocket.Get you must remember to have it like in example below. If you try to use only something like HTTPSocket.get(string,f,10) the class will not react for displaying the download progress or other Event Handlers.

One tip more – When you are using synchronous HTTP, your application may need to wait a noticeable amount of time to get or post a page. Setting Yield to True allows for events and other code to execute while waiting for a page.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Dim d As New hget
Dim f As FolderItem
Dim filename As String
 
f = SpecialFolder.Desktop.Child(filename) // saves the file on Desktop
 
d.Yield = True
 
if f.Exists = false then //if file doesn't exists then 
    
  Window1.Listbox1.AddRow "Creating file: " + Str(f.DisplayName)
 
  d.get (TextField1.Text,f) //downloading file  to desktop
 
elseif f.Exists = true then
  Return
end

So to get everything work combine our two last methods into one and insert them to PushButton1 Sub Action () like here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Dim d As New hget
Dim f As FolderItem
Dim i, n As Integer
Dim s, a, filename As String
Dim b As Boolean
 
b = true
 
  for i = 0 to Len(TextField1.Text)
 
    s = Right(TextField1.Text, i)
 
    a = Mid(s, 1 ,1)
 
    if a = "/" and b = true then
 
      b= false
      n = i
 
    end
 
  next
 
  filename = Right(TextField1.Text, n - 1)
 
  f = SpecialFolder.Desktop.Child(filename)
 
  d.Yield = True
 
  if f.Exists = false then
 
    Window1.Listbox1.AddRow "Creating file: " + Str(f.DisplayName)
 
    d.get (TextField1.Text,f)
 
  elseif f.Exists = true then
    Return
end

Download

If you see this message you probably want access special material for Premium Members (Source Files, Bonus Tutorials, free Classes and More). You can do this by subscribing REALbasic City site for 10 USD (access for 3 months). There is also a option to buy access only to this material. Login or register to get option to buy and support REALbasic City.

Log in

Author: Jakub Pawlak

I'm interested in all. Currently working on the development of TUTSPOLIS and I hope that in near feature the project will be successful.

12 Responses to “Download Manager in REALbasic”

  1. fra says:

    please give me you suourcecode?

  2. Jakub Pawlak says:

    Hello!

    I’m working on a system to deliver source code download (mac os, windows, linux versions) options for 2 USD per month. The special accounts can offer also maybe some bonus materials, more tricks in standard tutorials, discounts offer for REALbasic, maybe some conquest to win some plugins etc.

    I hope that 2 USD is a good price for this kind of bonus and it can relay make this site run always fresh and deliver even better tutorials.

  3. fra says:

    okay
    Thanks :)

  4. redmanu says:

    Premium Member: Expires in 90 days … after 90 days … paypal make an automatic payment for me? I want decide if before 90 days want to buy another 90 days … i don’t like automatic payment method

  5. Jakub Pawlak says:

    Hello redmanu,

    the only option to buy premium account that I can offer for you is PayPal Subscription. There is no other option than automatic payment method, so if you like to cancel the subscription you must go to PayPal account and click cancel subscription at any time you like. This way works PayPal Subscription and I can not do with this anything.

    Best Regards
    Jakub

    PS. New tutorials will be released in first days of May :)

  6. Tracy Valleau says:

    OK… I’ve looked all over this site trying to find a way to join, and get the source code for this downloader. Shop takes me to the RB site; the home page offers no way of joining… will you please point me at how to join? (And may I suggest that you make it easier? :-)

  7. Jakub Pawlak says:

    Hello Tracy Valleau,

    to join you must create a account on REALbasic City and login into it. This option you can see on end of every article. Next when you are logged in you will see in the same place a option to subscribe or buy a access only to specific material.

    Best Regards
    Jakub

  8. tvalleau says:

    Sorry… all I saw was a WordPress Blog login. So I registered for the blog. (That confused me, since I didn’t see the little “register” at first.) From signing in there I can go to Tutspolis. From there I can click on “our sites” and visit REALBasic city, which is http://realbasic.tutspolis.com/... but I still don’t see anything about subscribing…. just a list of recent articles.

  9. Jakub Pawlak says:

    If you are logged in to Tutspolis you are also now logged in to REALBasic city. Now you can see in the same place a option to subscribe or buy a access only to specific material. There should by no login button.

  10. tvalleau says:

    I’m sorry. I’m really NOT trying to be difficult or stupid. I’ve been a programmer since 1978, and on the internet since the day it was opened to the public…. I visit the URLs and there is NOTHING there about subscribing. I use Find in Safari, and the words “subscribe” or “access” or “purchase” or “buy” don’t appear anywhere on the page. Can I send you a screenshot? Better, can you please just post the URL here?

    Consider me to be dumb, or blind. Please visit http://realbasic.tutspolis.com/ or http://tutspolis.com/ and point out to me where the subscribe/join/buy/access link is. I simply do not see any!

  11. tvalleau says:

    OOOOOHHHH… (forgive me, but – weird…) First, yes, now I see the option to purchase the article or sign up. I will sign up.

    But, I’ve been marketing on the internet for many, many years, and I’ve never seen this kind of setup.

    Honestly, my advice is that on every one of your pages, right at the top, in that colored bar (articles, classes, plugins etc) you have a nice big JOIN. Let that take a user right to a page with a paypal button on it. Make it simple for people to subscribe. Put it, in big, obvious, letters right at the top of very page. I’ll bet your paid registrations go way up almost instantly!

    AJAX is sweet, but burying your access in little links, part way down the page, on a self-modifying screen -really isn’t- the best marketing technique! :-)

    Sent with good cheer, in the hopes that my 30+ years of web marketing will help you succeed.

    Now I’ll scroll back up the page, and join…

Leave a Reply

Copyrights (c) TUTSPOLIS | Powered by Wordpress MU | Inspired at Elegant Themes designed by qbap (HTML5 & CSS3)