我一直在尝试使用 Android 移动设备中的 Phonegap 以编程方式将(zip)文件上传到远程服务器。我尝试了FileAPI文档,找到了解决方案here。但它似乎没有用。但是,我可以成功上传图像(使用相机和导航器),如示例中所述。
我在 SD卡的test.zip
文件夹中有一个文件test
。我需要将此文件上传到远程服务器。
对此有任何帮助都很棒。
答案 0 :(得分: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);
};
}