【发布时间】:2018-03-09 22:44:16
【问题描述】:
我目前所做的想法是,您可以拍照或从图库中选择一张并将其上传到服务器。我在使用科尔多瓦FileTransfer 发送和上传图片时遇到了麻烦。要么根本没有发送,要么$_FILES["file"] 为空。
我有单独的按钮来调出相机和画廊:
<button id="takePicture" name="takePicture" ng-click="openCamera();">Take Photo</button>
<button id="getPicture" name="getPicture" ng-click="openGallery();">Choose From Gallery</button>
解决方案:
$scope.openCamera = function()
{
navigator.camera.getPicture(onSuccess, onError,
{ quality : 100,
destinationType : Camera.DestinationType.FILE_URI
});
function onSuccess(imageURI)
{
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = imageURI.substr(imageURI.lastIndexOf("/")+1);
options.mimeType = "image/jpeg";
options.httpMethod = "POST";
options.chunkedMode = false;
options.params = { filePath : imageURI.split("?")[0] };
var fileTransfer = new FileTransfer;
fileTransfer.upload(imageURI, encodeURI("upload.php"), uploadComplete, uploadError, options);
function uploadComplete(result)
{
console.log("Code = " + result.responseCode);
console.log("Response = " + result.response);
console.log("Sent = " + result.bytesSent);
}
function uploadError(error)
{
alert("An error has occurred: Code = " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
}
}
function onError(message)
{
alert("fail");
alert('Failed because: ' + message);
}
}
然后将在upload.php 文件中接收发送的数据。您应该能够通过检查文件var_dump($_FILES);来检查数据是否已发送
正如Sletheren 提到的,也可以使用$cordovaFileTransfer.upload(); 来完成
【问题讨论】:
标签: angularjs cordova file-transfer