2

我正在使用 Phonegap 文件上传将 SVG 文件上传到我的服务器。它工作正常。但我需要从 iPad 的绝对路径中获取所有 SVG 文件并将它们发送到我的服务器。我不知道如何使用 Phonegap 的文件 API 获取所有 .svg 文件,以便我可以通过文件名循环发送到服务器。请告诉我如何做到这一点。

我的文件上传代码是:

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
  window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
  fileSystem.root.getFile("image5_2.jpg.svg", {create: true, exclusive: false}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
  var localpath=fileEntry.fullPath;
  uploadPhoto(localpath);
}
function uploadPhoto(imageURI) {
  var options = new FileUploadOptions();
  var ft = new FileTransfer();
  ft.upload(imageURI, "http://192.168.1.54:8080/POC/fileUploader", win, fail, options);
}
4

1 回答 1

7

您需要从根 FileSystem 创建一个DirectoryReader并遍历所有条目以查找 .svg 文件。

function gotFS(fileSystem) {
    var reader = fileSystem.root.createReader();
    reader.readEntries(gotList, fail);    
}

function gotList(entries) {
    var i;
    for (i=0; i<entries.length; i++) {
        if (entries[i].name.indexOf(".svg") != -1) {
            uploadPhoto(entries[i].fullPath);
        }
    }
}

您可能需要对此代码进行一些小的编辑,但它应该可以帮助您入门。

于 2012-06-16T16:38:08.243 回答