I am doing project in android phonegap and I want to upload pic to the server.
But I am not getting idea, where should I put this code.
I can't show any buttons to upload photos, please help.
I am new in this. I refereed this code from phonegap documentation.
I am trying this for hours, but can't get the better solution.
It's my first android phonegap project.
Code:
<head>
<script type="text/javascript" charset="utf-8" src="cordova-2.4.0.js"></script>
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
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.params = params;
var ft = new FileTransfer();
ft.upload(imageURI, encodeURI("http://some.server.com/upload.php"), win, fail, 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);
}
</script>
</head>
<body>
<h1>Example</h1>
<p>Upload File</p>
</body>
This is one sample application which I did to upload a pic to server
Hope this helps
Thanks AB
Well, It's work for me.
The last parameter add true how boolean.
Before
After
You solve your problem using the next code:
See more info on this post: https://stackoverflow.com/a/13862151/1853864
Try the following code.
Create a button in your HTML page, on it's
onclick
event call following functions as per your requirement.capturePhoto()
function to capture and upload photo.getPhoto()
function to get image from gallery.HTML should contain a buttons like:
Hope that helps.
enter link description here
This is a link for 100% working method for uploading multiple image to server via cordova or phonegap function using html java script
Multiple Image upload to server in Cordova