1

我想从 Amazon S3 服务器获取数据(音频文件),并将其存储在便携式设备的本机文件系统中。

我将使用 PhoneGap ( PhoneGap - Wiki - Filesystem ) 来存储数据,但我如何发出 GET 请求并处理返回的文件并将其保存到文件系统?

4

1 回答 1

3

以下一组函数可能会有所帮助:

function onDeviceReady() {
    console.log("device is ready");
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}

function fail() {
    console.log("failed to get filesystem");
}

function gotFS(fileSystem) {
    console.log("got filesystem");

    // for later use
    window.fileSystem = fileSystem;

    // Create your app specific dir here
    fileSystem.root.getDirectory(".myapp", {
        create: true,
        exclusive: false
    },
    dirReady, fail);
}

function dirReady(entry) {
    // for later use
    window.appRootDir = entry;
    console.log(JSON.stringify(window.appRootDir));

    var fileToDownload = "http://....";
    var fileName = "file name";
    downloadToAppRoot(fileToDownload, fileName,
    function() {},
    function() {});
}

function downloadToAppRoot(url, name, success, failure) {
    var fileTransfer = new FileTransfer();
    var filePath = window.appRootDir.fullPath + "/" +  name;
    fileTransfer.download(
    url,
    filePath,
    function(entry) {
        console.log("download complete: " + entry.fullPath);
        success(entry);
    },
    function(error) {
        console.log("download error source " + error.source);
        if (failure) failure(error);
    }
    );
}

PhoneGap 具有广泛的文件系统 API 来创建目录和文件。有关更多详细信息,您可以在此处查看 PhoneGap 文档

于 2012-06-06T19:15:01.853 回答