1

How can I download a file and store it locally? I've searched the doc and google and couldn't find an example of it.

I tried this:

this.copyRemote = function(path,path2){
    reader = Ti.Network.createHTTPClient();
    writer = Ti.Filesystem.getFile(path2);

    reader.open('GET',path);
    reader.receive(writer);
}

But Tidesdk crashes while trying to download the file, the last messages on console are:

[12:42:39:647] [Ti.Network.HTTPClient] [Debug] Changing readyState from 0 to 1 for url:https://buttonpublish.com/api/images/7/image257189x142.jpg
[12:42:39:671] [Ti.Proxy] [Debug] Looking up proxy information for: https://buttonpublish.com/api/images/7/image257189x142.jpg
[12:
madth3
  • 7,275
  • 12
  • 50
  • 74
RollWhisTler
  • 341
  • 1
  • 3
  • 10

2 Answers2

2

Seems like there has been success on the TideSDK Google Group using the code below:

var httpClient = Ti.Network.createHTTPClient();
httpClient.open('GET', path);
httpClient.receive(function(data) {
  var file = Ti.Filesystem.getFile(path2);
  var fileStream = file.open(Ti.Filesystem.MODE_APPEND);
  fileStream.write(data);
  fileStream.close();
});

Hope that helps, at least to point in the right direction.

David M.
  • 773
  • 4
  • 13
0

I found that this works for my needs, which are just to get the file onto the machine:

function downloadFile( url ){
    Ti.Platform.openApplication( url );
}

This opens the URL using the machine's default browser. One downside to this approach is that the user is normally prompted to confirm the download. I use the downloadFile function in case I want to change how this works in the future.

Femi
  • 1,332
  • 9
  • 20