downloader

473 views
Skip to first unread message

nikhil baby

unread,
Oct 18, 2015, 6:45:35 AM10/18/15
to AndroidScript
how to download a file from a url in a downloader .

Steve Garman

unread,
Oct 18, 2015, 7:31:45 AM10/18/15
to AndroidScript

function OnStart()
{
    var srcFileUrl="http://sgarman.net/downloadable.txt";
    var targetDir="/sdcard";

    dload = app.CreateDownloader();
    dload.SetOnComplete( dload_OnComplete );
    dload.Download( srcFileUrl, targetDir );
}

function dload_OnComplete()
{
     var fil="/sdcard/downloadable.txt";
     if(app.FileExists(fil))
         app.ShowPopup(app.ReadFile(fil));
     else app.ShowPopup("Download failed");
}

Chantha Chhai

unread,
May 12, 2016, 7:43:07 AM5/12/16
to DroidScript
I try to download "srcFileUrl="http://192.168.173.228:8080/Download/KH%20121/Track%2003.mp3"; "
to my "targetDir="/sdcard";"
But it fail forever.

was play it correctly.


thanks!

Gerard Hernandez

unread,
May 12, 2016, 10:30:38 AM5/12/16
to DroidScript
I wonder are you using the downloader and the server in the same code??  Or what is the setting your have?

Chantha Chhai

unread,
May 12, 2016, 11:37:53 AM5/12/16
to DroidScript

//Called when application is started.
function OnStart()
{
//Check wifi is enabled.
var ip = app.GetIPAddress();
if( ip == "0.0.0.0" ) {
alert( "Please Enable Wi-Fi" );
app.Exit();
}

var srcFileUrl="http://192.168.173.228:8080/Download/KH%20121/Track%2003.mp3";
var targetDir="/sdcard";

dload = app.CreateDownloader();
  
  dload.SetOnComplete( dload_OnComplete );
   dload.Download( srcFileUrl, targetDir );

function dload_OnComplete()
{

var fil="/sdcard/KH 121/Track 03.mp3";
if(app.FileExists(fil))
app.ShowPopup(app.ReadFile(fil));
else app.ShowPopup("Download failed");

}




//Create a layout with objects vertically centered.
lay = app.CreateLayout( "linear", "VCenter,FillXY" );

//Create a text label and add it to layout.
var s = "Type the following address into your" +
" browser\n\n" + ip +":8080";
txt = app.CreateText( s, 0.8, 0.5, "MultiLine" );
txt.SetTextSize( 22 );
lay.AddChild( txt );

//Add layout to app.
app.AddLayout( lay );

//Create and run web server.
serv = app.CreateWebServer( 8080, "Upload,ListDir" );
serv.SetFolder( "/sdcard" );
serv.AddServlet( "/message", OnServlet );
serv.Start();
}

//Handle servlet requests.
function OnServlet( request, info )
{
serv.SetResponse( "Got it!" );
app.ShowPopup( info.remoteAddress + " says: " + request.msg );
}

Chantha Chhai

unread,
May 12, 2016, 11:40:20 AM5/12/16
to DroidScript
It fail to download.

Gerard Hernandez

unread,
May 12, 2016, 12:57:48 PM5/12/16
to DroidScript
you are trying to download after the server is enabled, I would recommend to delay the execution with a timeout, that way you can start the server, wait 2-3 secs and then download.

Also make sure dload_OnComplete is a global function not declared inside the OnStart method.

Reply all
Reply to author
Forward
0 new messages