我一直在尝试通过 PhoneGap (Cordova) 设置一个应用程序来拍摄图像并将它们上传到我们的服务器。我在这里浏览了很多回复并尝试了其中的代码。我可以拿起相机拍照,甚至可以访问手机图库。但我无法将图像发送到服务器。我试过发送图像,甚至发送 base64 图像流。我无法将它发送到服务器。
这是客户端的javascript:
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
}
function ImageUpload() {
this.useExistingPhoto = function(e) {
this.capture(Camera.PictureSourceType.SAVEDPHOTOALBUM);
}
this.takePhoto = function(e) {
this.capture(Camera.PictureSourceType.CAMERA);
}
this.capture = function(sourceType) {
navigator.camera.getPicture(this.onCaptureSuccess, this.onCaptureFaile, {
destinationType: Camera.DestinationType.FILE_URI,
soureType: sourceType,
correctOrientation: true
});
}
this.onCaptureSuccess = function(imageURI) {
var fail, ft, options, params, win;
success = function(response) {
alert("Your photo has been uploaded!");
};
fail = function(error) {
alert("An error has occurred: Code = " + error.code + "\nMessage = "+error.message);
};
options = new FailUploadOptions();
options.fileKey = "file";
options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
options.mimeType = "text/plain";
params = {
val1: "some value",
val2: "some other value"
};
options.params = params;
ft= new FileTransfer();
ft.upload(imageURI, 'http://style.appdev01.com/app/client-profile.php', success, faile, options);
}
this.OnCaptureFail = function(message) {
alert("Failed because: "+message);
}
};
var imageuploader = new ImageUpload();
点击时有两个按钮调用 imageuploader.takePhoto 和 .useExistingPhoto。
在服务器端我有这个 php: if(isset($_FILES['file'])) {
$target_path = "/home/style/public_html/images/client_images/app_image.jpg";
move_uploaded_file($_FILES['file']['tmp_name'], $target_path);
$insert = "INSERT INTO
`fut`
SET
`request` = '".serialize($_POST)."',
`file` = '".serialize($_FILES)."'";
$mysql->query($insert);
}
这只是将 POST 和 FILE 数组存储到数据库,以确保它们通过并创建图像。
但同样,没有任何东西可以到达服务器。任何帮助将不胜感激。我已经从这里和整个网络上的很多问题中尝试了很多版本的代码。