我想通过Phonegap 3.7.0将图像文件从photolibrary(iOS和Android)传输到php。我在Android上遇到Android代码错误null和代码错误3,我尝试了多种解决方案,但这对我没有用。下面的Phonegap Javascript来自官方的Phonegap文档,所以我不知道我做错了什么。
我希望有人可以帮助我,
提前致谢!
HTML:
<input ng-click="onDeviceReady()" type="image" class="addphoto" ng-src="img/addPhotoButton.png" id="photo1" alt="addPhotoButton"/>
使用Javascript:
$scope.onDeviceReady = function() {
// Retrieve image file location from specified source
navigator.camera.getPicture(
uploadPhoto,
function(message) { alert('get picture failed'); },
{
quality : 50,
destinationType : navigator.camera.DestinationType.FILE_URI,
sourceType : navigator.camera.PictureSourceType.PHOTOLIBRARY
}
);
}
function uploadPhoto(imageURI) {
var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
options.mimeType="image/jpeg";
var params = {};
params.value1 = "test";
params.value2 = "param";
options.chunkedMode = false;
options.headers = { Connection: "close"};
options.params = params;
var ft = new FileTransfer();
ft.upload(imageURI, "photo.php", win, fail, options);
console.log(imageURI);
console.log(params);
console.log(options);
}
function win(r) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
}
function fail(error) {
alert("An error has occurred: Code = " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
}
PHP:
function postPhoto(){
print_r($_FILES);
$new_image_name = "photo1.jpg";
move_uploaded_file($_FILES["file"]["tmp_name"], "/temp".$new_image_name);
}
Phonegap xml配置文件:
<gap:plugin name="org.apache.cordova.camera"/>
<gap:plugin name="org.apache.cordova.media-capture"/>
<gap:plugin name="org.apache.cordova.file"/>
<gap:plugin name="org.apache.cordova.file-transfer"/>
<gap:plugin name="org.apache.cordova.globalization"/>
<gap:plugin name="org.apache.cordova.media"/>
<access origin="*" />
<!-- Features -->
<feature name="Accelerometer">
<param name="ios-package" value="CDVAccelerometer" />
</feature>
<feature name="Geolocation">
<param name="ios-package" value="CDVLocation" />
</feature>
<feature name="Geolocation">
<param name="android-package" value="org.apache.cordova.geolocation.GeoBroker" />
</feature>
<feature name="Camera">
<param name="android-package" value="org.apache.cordova.camera.CameraLauncher" />
</feature>
<feature name="Camera">
<param name="ios-package" value="CDVCamera" />
</feature>
<feature name="File">
<param name="android-package" value="org.apache.cordova.FileUtils" />
</feature>
<feature name="FileTransfer">
<param name="android-package" value="org.apache.cordova.FileTransfer" />
</feature>
<feature name="File">
<param name="ios-package" value="CDVFile" />
</feature>
<feature name="FileTransfer">
<param name="ios-package" value="CDVFileTransfer" />
</feature>
<feature name="http://api.phonegap.com/1.0/camera" />
<feature name="http://api.phonegap.com/1.0/file" />
<feature name="http://api.phonegap.com/1.0/geolocation"/>
<feature name="http://api.phonegap.com/1.0/network" />