5

我一直在尝试使用Android手机中的Phonegap以编程方式将(zip)文件上传到远程服务器。我已经尝试过FileAPI文档和在此处找到的解决方案。但这似乎不起作用。但是,我可以按照示例中的说明成功上传图像(使用相机和导航器​​)。

我在SD 卡test.zip的文件夹中有一个文件。我需要将此文件上传到远程服务器。test

任何有关这方面的帮助都会很棒。

4

1 回答 1

3

我得到它的工作,这是我使用的代码

uploadFile('test.zip', 'Test', 'multipart/x-zip');

function uploadFile(fileName, dirName, fileMime) {

    var win = function(r) {
        console.log("Code = " + r.responseCode);
        console.log("Response = " + r.response);
        console.log("Sent = " + r.bytesSent);
        alert(r.response);
    };

    var fail = function(error) {
        alert("An error has occurred: Code = " = error.code);
    };

    var fileURI;

    var gotFileSystem = function(fileSystem) {
        fileSystem.root.getDirectory(dirName, {
            create : false
        }, function(dataDir) {

            fileURI = dataDir.fullPath;
            fileURI = fileURI + '/' + fileName;

            alert(fileURI);

            var options = new FileUploadOptions();
            options.fileKey = "file";
            options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);
            options.mimeType = fileMime;

            var params = new Object();
            params.value1 = "test";
            params.value2 = "param";

            options.params = params;

            var ft = new FileTransfer();
            ft.upload(fileURI,

                    // Enter the server url
                    "http://example.com/upload.php", win,
                    fail, options);

        }, dirFail);

    };

    // file system fail
    var fsFail = function(error) {
        alert("failed with error code: " + error.code);

    };

    // get file system to copy or move image file to
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFileSystem,
            fsFail);

    var dirFail = function(error) {
        alert("Directory error code: " + error.code);

    };
}
于 2012-05-31T07:04:03.137 回答