我正在使用带有 PhoneGap 的 Eclipse 来开发应用程序。对于运行配置,我使用的是 Android 设备,因为它比使用 AVD 更快。
所以,我想做的是从手机(通过笔记本电脑连接到 Eclipse 的 USB)上传文件到我的本地主机服务器。主要是把文件上传到远程服务器。但至于最初的部分,我只想将它上传到本地主机。
(index.html) 这是我从 HTML 上传的脚本:
<!DOCTYPE html>
<html>
<head>
<title>Capture Photo</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
var pictureSource; // picture source
var destinationType; // sets the format of returned value
document.addEventListener("deviceready",onDeviceReady,false);
function onDeviceReady() {
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.DestinationType;
}
function onPhotoDataSuccess(imageData) {
var smallImage = document.getElementById('smallImage');
smallImage.style.display = 'block';
smallImage.src = "data:image/jpeg;base64," + imageData;
var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=imageData.substr(imageData.lastIndexOf('/')+1);
options.mimeType="image/jpeg";
console.log("Filename: " + options.fileName);
var params = new Object();
params.value1 = "test";
params.value2 = "param";
options.params = params;
options.chunkedMode = false;
var ft = new FileTransfer();
ft.upload(imageData, "http://127.0.0.1:80/upload_file.php", win, fail, options);
}
function capturePhoto() {
navigator.camera.getPicture(onPhotoDataSuccess, onFail,
{
quality: 50,
sourceType : Camera.PictureSourceType.CAMERA,
destinationType: destinationType.DATA_URL
}
);
}
function win(r) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
alert(r.response);
}
function fail(error) {
alert("An error has occurred: Code = " = error.code);
}
function onFail(message) {
alert('Failed because: ' + message);
}
</script>
</head>
<body>
<button onclick="capturePhoto();">Capture Photo</button> <br>
<img style="display:none;width:300px" id="smallImage" src="" />
<img style="display:none;" id="largeImage" src="" />
</body>
</html>
是的,代码的作用是用相机拍摄照片,然后将照片上传到服务器。但我主要关注的是上传它。
(c:/wamp/www/upload.php) 这是服务器端的 php 脚本:
<?php
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "";
} else {
echo "Upload: " . $_FILES["file"]["name"] . "";
echo "Type: " . $_FILES["file"]["type"] . "";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "";
if (file_exists("D:/PHPUPLOAD/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"], "C:/PHPUPLOAD/" . $_FILES["file"]["name"]); //Save location
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
?>
如果我使用 Android 设备而不是 AVD 应该没问题吧?