<!DOCTYPE HTML>
<html>
<head>
<title> HellowWorld PhoneGap App </title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript">
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
alert("Ready ");
}
function capture(){
navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
destinationType: Camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
});
}
var image = document.getElementById('myImage');
\t \t image.src = imageData;
\t \t var blob = image[0].getAsFile();
\t \t window.URL = window.URL || window.webkitURL;
\t \t var blobUrl = window.URL.createObjectURL(blob);
\t \t var file = blob;
\t \t upload(file);
function uploadImage(file){
\t \t
var fd = new FormData();
fd.append("file", file);
var xhr = new XMLHttpRequest();
xhr.open('POST', 'uploads/upload.php', true);
xhr.send(fd);
}
function onUploadSuccess(){
alert('Photo Uploaded Successfully');
}
function onUploadError(){
alert('Error uploading photo');
}
function onSuccess(imageData) {
var image = document.getElementById('myImage');
image.src = imageData;
uploadImage(imageData);
}
function onFail(message) {
alert('Failed because: ' + message);
}
</script>
</head>
<body>
<h1>Upload images to remote server</h1>
<div class="app">
<h4><a href="#" onclick="capture();">Select an Image</a></h4>
<img id="myImage" src="" width="50%">
</div>
</body>
</html>
<?php
$destination = 'uploads/';
if (isset($_FILES["file"]["name"])) {
$name = $_FILES["file"]["name"];
$tmp_name = $_FILES['file']['tmp_name'];
$error = $_FILES['file']['error'];
//echo $name;
//echo $tmp_name;
//echo $error;
move_uploaded_file($_FILES['file']['tmp_name'], $destination.$name);
}
?>
,所以我嘗試創建一個PhoneGap的應用將圖片上傳到服務器。我首先創建了一個上傳文件夾並粘貼了upload.php。 javascript和html是index.html。我試着跑步,它允許我拍照並查看照片,但上傳照片時出現錯誤。怎麼來的?我寫錯了什麼?
<?php
print_r($_FILES);
move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/". $_FILES["file"]["name"]);
?>
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
alert("Ready ");
}
function capture() {
navigator.camera.getPicture(onSuccess, onFail, {
quality: 50,
destinationType: Camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
});
}
function uploadImage(imageData) {
var serverURL = "localhost:3000/uploads/upload.php";
var options = new FileUploadOptions();
options.fileKey = 'file';
options.fileName = imageData.substr(imageData.lastIndexOf('/') + 1);
options.mimeType = "image/jpeg";
var ft = new FileTransfer();
ft.upload(imageData, serverURL, onUploadSuccess, onUploadError, options);
}
function onUploadSuccess() {
alert('Photo Uploaded Successfully');
}
function onUploadError() {
alert('Error uploading photo');
}
function onSuccess(imageData) {
var image = document.getElementById('myImage');
image.src = imageData;
uploadImage(imageData);
}
function onFail(message) {
alert('Failed because: ' + message);
}
<!DOCTYPE HTML>
<html>
<head>
<title> HellowWorld PhoneGap App </title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
</head>
<body>
<h1>Upload images to remote server</h1>
<div class="app">
<h4><a href="#" onclick="capture();">Select an Image</a></h4>
<img id="myImage" src="" width="50%">
</div>
</body>
</html>
什麼錯誤,請具體說明 – RiggsFolly
@RiggsFolly 功能onUploadError(){ 警報(「上傳照片時出錯」); } 這個錯誤 – user7569149
你在爲android建設嗎? – Harish