I'm using Eclipse with PhoneGap to develop an app. For the run config, I'm using Android Device because it is faster compared to using AVD.
So, what I wanna do is to upload a file from the phone (USB connected to eclipse via laptop) to my localhost server. Mainly speaking, to upload a file to a remote server. But as for the initial part, I just want to upload it to the localhost.
(index.html) Here is my upload script from 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>
And yes, what the code does is it captures a photo with camera and that photo will be uploaded to the server. But my main thing to be focused here is to upload it.
(c:/wamp/www/upload.php) Here is the php script for the server-side:
<?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"];
}
}
?>
It should be okay right if I'm using Android Device instead of AVD?