1

如何下载文件并将其存储在本地?我搜索了文档和谷歌,找不到它的例子。

我试过这个:

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

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

但是 Tidesdk 在尝试下载文件时崩溃,控制台上的最后一条消息是:

[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:
4

2 回答 2

2

似乎使用以下代码在TideSDK Google Group上取得了成功:

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();
});

希望这会有所帮助,至少可以指出正确的方向。

于 2013-02-02T04:22:16.393 回答
0

我发现这可以满足我的需要,这只是为了将文件放到机器上:

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

这将使用机器的默认浏览器打开 URL。这种方法的一个缺点是通常会提示用户确认下载。我使用 downloadFile 功能,以防我将来想更改它的工作方式。

于 2013-05-03T15:01:58.053 回答