5

I have been trying to upload a (zip) file to a remote server programmatically using Phonegap in Android mobile. I have tried the FileAPI documentation and the solution found here. But it doesn't seem to work. However I could successfully upload an image (using the camera and navigator) as described in the examples.

I have a file test.zip in a folder test in the SD Card. I need to upload this file to a remote server.

Any help regarding this would be great.

4
  • Is it bcoz of the wrong URI ? IS there a way to get the correct URI for the SD Card ? Tried with 'file:///test/test.zip' and 'content://media/external/test/test.zip' Commented May 29, 2012 at 13:07
  • The FileAPI pointed out by you says that in case of an error. the error will be reported in error callback with FileTransferError object. What does that object say? Commented May 29, 2012 at 13:12
  • Getting an error "Error in error callback: FileTransfer3 = ReferenceError: Invalid left-hand side in assignment". But I can succesfully upload an image, if FILEURI given as "content://media/external/images/media/963". I assume something wrong with my zip fileURI ? Commented May 29, 2012 at 13:55
  • I haven't used Phonegap myself, but to me it seems that there is an error in the error callback function itself. Since the image is uploaded successfully, the error callback is never called then. In case of zip file upload there is an error but is currently hidden behind the error in error callback! Commented May 29, 2012 at 14:17

1 Answer 1

3

I got it working, here is the code that I used

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("An error has occurred: Code = " = error.code); }; var fileURI; var gotFileSystem = function(fileSystem) { fileSystem.root.getDirectory(dirName, { create : false }, function(dataDir) { fileURI = dataDir.fullPath; fileURI = fileURI + '/' + fileName; alert(fileURI); 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, gotFileSystem, fsFail); var dirFail = function(error) { alert("Directory error code: " + error.code); }; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.