我正在使用cordova-file-transfer插件将文件上传到服务器。我知道我的文件上传在使用浏览器选择文件时有效但我对应用程序并不完全确定,因为这是一种相对较新的方式,无需提交表单即可为我做。
编辑:下面的代码被修改,并且在遵循@ kay27的建议之后是我成功的。解决方案是使用params将数据POST到等待上传处理程序。
function uploadFile() {
function success(r) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
}
function fail(error) {
console.log("An error has occurred: Code = " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
}
/* Destination of file */
var url = encodeURI("http://someURL/yourPHPUploadFile.php");
var fileURI = "file:///storage/emulated/0/Android/data/com.yourPackageName/fileToUpload";
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);
options.mimeType = "text/csv";
var params = new Object();
//allows you to POST the data to server side script
params.fileName = options.fileName;
options.params = params;
var ft = new FileTransfer();
ft.upload(fileURI, url, success, fail, options);
}
已编辑的PHP表单
<?php
header('Access-Control-Allow-Origin: *');
$location = "uploads/";
$uploadfile = $_POST['fileName'];
$uploadfilename = $_FILES['file']['tmp_name'];
if (move_uploaded_file($uploadfilename, $location . '/' . $uploadfile)) {
echo 'File successfully uploaded!';
} else {
echo 'Upload error!';
}
?>
答案 0 :(得分:0)
对于我的特定问题,定义params的新方法有效。可以在Simon MacDonald's answer上看到解决方案。我已经使用允许我上传的最终解决方案编辑了上面的代码。