Reputation: 743
I am creating an app in the phonegap android in which i want to upload a file to the server. What i want is when an user will click on the upload button an option dialog will open from where the user will select a file to be uploaded and then when user click on the save button the file will be uploaded. The dialog will be like normal window same as we see when we click on the attach button in the mail in the desktop. Can anyone tell me how to do this in android mobile? Any help is appreciated.
Thanks in advance.
Upvotes: 4
Views: 7717
Reputation: 3207
Use phonegap file transfer method The FileTransfer object allows you to upload or download files to and from a server.
Properties
onprogress: Called with a ProgressEvent whenever a new chunk of data is transferred. (Function)
Methods
upload: sends a file to a server.
download: downloads a file from server.
abort: Aborts an in-progress transfer.
Sample // !! Assumes variable fileURI contains a valid URI to a text file on the device
var win = function (r) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
}
var fail = function (error) {
alert("An error has occurred: Code = " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
}
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);
options.mimeType = "text/plain";
var params = {};
params.value1 = "test";
params.value2 = "param";
options.params = params;
var ft = new FileTransfer();
ft.upload(fileURI, encodeURI("http://some.server.com/upload.php"), win, fail, options);
You can browse and select a file using
var source = navigator.camera.PictureSourceType.PHOTOLIBRARY;
navigator.camera.getPicture(successFn, errorFn, { quality: 50,
destinationType: this.photoDestinationType.FILE_URI,
sourceType: source,
mediaType: navigator.camera.MediaType.ALLMEDIA });
more information Check link
Upvotes: 4