Cordova文件上传接收结束
Receiving end of Cordova file upload
我正在使用 cordova-file-transfer 插件将文件上传到服务器。我知道我的文件上传在使用浏览器 select 文件时有效,但我不完全确定该应用程序,因为它是一种相对较新的方式,无需提交表单。
编辑:下面的代码经过修改,在遵循@kay27 的建议后对我来说是成功的。解决方案是使用参数 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!';
}
?>
对于我的特定问题,定义参数的新方法有效。可以在 Simon MacDonald's answer 上查看解决方案。我用允许我上传的最终解决方案编辑了上面的代码。
我正在使用 cordova-file-transfer 插件将文件上传到服务器。我知道我的文件上传在使用浏览器 select 文件时有效,但我不完全确定该应用程序,因为它是一种相对较新的方式,无需提交表单。
编辑:下面的代码经过修改,在遵循@kay27 的建议后对我来说是成功的。解决方案是使用参数 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!';
}
?>
对于我的特定问题,定义参数的新方法有效。可以在 Simon MacDonald's answer 上查看解决方案。我用允许我上传的最终解决方案编辑了上面的代码。