Last active
December 19, 2015 05:09
-
-
Save 0xnbk/5902728 to your computer and use it in GitHub Desktop.
Programmatically upload a file from SD Card to a remote server using Phonegap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| uploadFile('test.zip', 'Test', 'multipart/x-zip'); | |
| function uploadFile(fileName, dirName, fileMime) { | |
| var win = function(r) { | |
| console.log("Code = " + r.responseCode); | |
| console.log("Response = " + r.response); | |
| console.log("Sent = " + r.bytesSent); | |
| alert(r.response); | |
| }; | |
| var fail = function(error) { | |
| alert("Error Code = " + error.code); | |
| }; | |
| var fileURI; | |
| var gotFileSystem = function(fileSystem) { | |
| fileSystem.root.getDirectory(dirName, { | |
| create : false | |
| }, function(dataDir) { | |
| fileURI = dataDir.fullPath; | |
| fileURI = fileURI + '/' + fileName; | |
| var options = new FileUploadOptions(); | |
| options.fileKey = "file"; | |
| options.fileName = fileURI.substr( | |
| fileURI.lastIndexOf('/')+ 1); | |
| options.mimeType = fileMime; | |
| var params = new Object(); | |
| params.value1 = "test"; | |
| params.value2 = "param"; | |
| options.params = params; | |
| var ft = new FileTransfer(); | |
| ft.upload(fileURI, | |
| // Enter the server url | |
| "http://example.com/upload.php", win, | |
| fail, options); | |
| }, dirFail); | |
| }; | |
| // file system fail | |
| var fsFail = function(error) { | |
| alert("failed with error code: " + error.code); | |
| }; | |
| // get file system to copy or move image file to | |
| window.requestFileSystem( | |
| LocalFileSystem.PERSISTENT, 0, | |
| otFileSystem,fsFail); | |
| var dirFail = function(error) { | |
| alert("Directory error code: " + error.code); | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment