How to upload any type of file to phonegap and jquery mobile?

I am creating an application on an Android phone where I want to upload a file to the server. What I want is when the user clicks the download button, a selection dialog opens, from which the user selects the file to download, and then, when the user clicks the save button, the file will be downloaded. The dialog will look like a regular window in the same way as we see when we click on the attach button in a message on the desktop. Can someone tell me how to do this on an Android phone? Any help is appreciated.

Thanks in advance.

+4
source share
1 answer

Use the phonebook file transfer method. The FileTransfer object allows you to upload or download files to and from the server.

The properties

  • onprogress: Invoked with a ProgressEvent whenever a new piece of data is wrapped. (Function)

Methods

  • upload: uploads the file to the server.

  • download: downloads a file from the server.

  • abort: aborts the transfer in the process.

Example // !! Assumes the fileURI variable contains a valid URI for 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 view 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 }); 

Additional information Check link

+4
source

Source: https://habr.com/ru/post/1499356/


All Articles