I am using $cordovaFileTraansfer plugin to upload a zip file to Node Server. Below is my Ionic code
return fileAPI.saveDataInFs(JSON.stringify(uploadJson), jsonFilename)
.then(function(data) {
return fileAPI.zipFiles(docList, zipFilename)
}).then(function(data) {
var targetPath = cordova.file.dataDirectory + $rootScope.username+ '/'+jsonFilename;
var trustHosts = true;
var options = {
mimeType:"application/octet-stream"
};
var server = config.nodeServerEndpoint + '/upload';
return $cordovaFileTransfer.upload(server, targetPath, options, trustHosts);
}).then(function (result) {
console.log("success", result);
}).catch(function(error) {
console.log('Error - '+JSON.stringify(error));
});
It’s a valid file on the iPad, however, once it gets uploaded to the Node server, it becomes corrupted.
When I try to unzip the file which is uploaded by $cordovaFileTransfer plugin, it gives me an error message stating
warning[abc.zip]: 172 extra bytes at the beginning or within zip file
I took the same zip file from iPad and uploaded it to node using Postman. This created a valid zip file on the node server.
Then I did a binary comparison of the zip file send through $cordovaFileTransfer (not working) and the one send through postman (working). Found that the one send through $cordovaFileTransfer has the following in the beginning of the zip file (which is making it invalid).
--+++++org.apache.cordova.formBoundary
Content-Disposition: form-data; name="file"; filename="image.jpg"
Content-Type: application/octet-stream
Content-Length: 22705
All the content is also there as a part of the (not working) zip, but these additional bytes in the beginning are making it invalid.
In my use case, user does not select the file that needs to be uploaded. Based on a user action (like button click), application would look for a zip file in a specific location on iPad and upload it using $cordovaFileTransfer.
What can I do to make $cordovaFileTransfer send files correctly?