Phonegap cli-5.2.0 File Transfer is not Working

  • 2
  • Problem
  • Updated 4 years ago
  • In Progress
Hi,

I am struggling to fix the issue using the file transfer plugin with cli-5.2.0 version.

The problem is while using the file transfer plugin with 5.2.0 version , the file is not downloading to the directory.

when i use phonegap 3.7.0 version its working fine, what is the problem with the cli-5.2.0 version

Here is o tried.
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccess, onError);

function onSuccess(fileSystem) {
// methods/properties for file path:
// fileSystem.name, fileSystem.root.toURL(), fileSystem.root.toInternalURL(), fileSystem.root.nativeURL

gFileSystemVar = fileSystem;
gPersistantPath = fileSystem.root.toInternalURL();
}
function onError(fileSystem) {
// Error ocurred while calling requestFileSystem.
//console.log("index js errror" + fileSystem.name);
}

fileTransfer.download(
uri,
fileURL,
function (entry) {
console.log( entry.toURL().) // it will print the downloaded file path
}
function (error) {
//Download error
console.log("download error source " + error.source);
},

but in download function the entry.toURL() is printing as file:///data/data//files/files.

but in the same path the file is not available.

Please help on this .
Thanks!!!
Photo of Siva Ram

Siva Ram

  • 5 Posts
  • 0 Reply Likes
  • sad

Posted 5 years ago

  • 2
Photo of Amir

Amir

  • 8261 Posts
  • 263 Reply Likes
Have you include and configure Whitelist plugin from NPM?
https://www.npmjs.com/package/cordova-plugin-whitelist
Photo of Siva Ram

Siva Ram

  • 5 Posts
  • 0 Reply Likes
I included the Whitelist plugin , but the problem with the i uses file transfer plugin.

There i am using fileSystem.root.toInternalURL() it get the internal storage access.

it is giving like file:///data/data/packagename/files/files . but if i opened the same directory in the mobile , file is not downloading.

So, My question is cli-5.2.0 version is on production stage or in testing stage.

How to fix the issue
Photo of Willy Quesada

Willy Quesada

  • 5 Posts
  • 0 Reply Likes
is there any update?
It seems i have a similar issue,i used the cordova.file.datadirectory in previous versions and everything was ok, since the phonegap version update, i only get undefined, when i try to use the cordova.file.datadirectory property.

Any help?
Photo of JesseMonroy650 (Volunteer)

JesseMonroy650 (Volunteer), Champion

  • 3325 Posts
  • 122 Reply Likes
Have you added CSP to your HTML file?
If not, Here is a quick fix
HOW TO apply the Cordova/Phonegap the whitelist system

If you are using iOS9, then you need to apply ATS. Also on that link.
Photo of Amir

Amir

  • 8261 Posts
  • 263 Reply Likes
Hey guys

Did Jesse' suggestion helps?
Photo of Willy Quesada

Willy Quesada

  • 5 Posts
  • 0 Reply Likes
Hi guys, unfortunately it didn`t, i can use the whitelist plugin with no problem whatsoever, the problem i think is with the definition of the cordova.file since i always get an undefined when i try to use it, this with the cli-5.2.0 version, but when i use it with the 3.7.0.
What i get from the link that Jesse sent, it is that i can only use the file transfer plugin with the 3.7.0 version, am i wrong?

Thanks for the help.
Photo of JesseMonroy650 (Volunteer)

JesseMonroy650 (Volunteer), Champion

  • 3325 Posts
  • 122 Reply Likes
What i get from the link that Jesse sent, it is that i can only use the file transfer plugin with the 3.7.0 version, am i wrong?


@Willy Quesada,
yes. you are wrong. I'm sorry, when I answered the question. I thought I was answering the original person.

When people join the thread and say, "I have the same problem". They rarely do.

At this point, we are going to see what you actually did.

I'm going to assume you have cli-5.2.0 set, or you did not set the compiler version (in which case you get cli-5.2.0).

Next, did you apply the whitelist, the plugin and CSP?
If so, what happened. If not, please try.

Jesse
Photo of Jade George

Jade George

  • 2 Posts
  • 0 Reply Likes
I'm pretty sure I have the same/a similar problem. In my apps I create a pdf by adding a canvas image sand save that pdf to the file system. It works fine in 3.7 but doesn't work in cli-5.2.0. So i worked back a bit and believe the problem lies in saving the file. I also can't open a pdf (can open but it is blank) that is linked to my app and think that this might be a related problem.

Simplified code to save a text file on button click:

function textFile () {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}

function gotFS(fileSystem) {
fileSystem.root.getFile("readme.txt", {create: true}, gotFileEntry, fail);
}

function gotFileEntry(fileEntry) {
fileEntry.createWriter(gotFileWriter, fail);
}

function gotFileWriter(writer) {
writer.onwrite = function(evt) {
alert("write success");
};
writer.onerror = function(evt) {
alert("write eroor " + evt);
};
writer.write("some sample text");
alert('added text');
// writer.abort();
// alert('writer abort');
// contents of file now 'some different text'
}

function fail(error) {
alert("error : "+error.code);
}


My config file contains:

<feature name="http://api.phonegap.com/1.0/camera"/>
<feature name="http://api.phonegap.com/1.0/file"/>
<feature name="http://api.phonegap.com/1.0/filetransfer" />
<feature name="http://api.phonegap.com/1.0/device"/>
<feature name="Canvas2ImagePlugin" >
<param name="android-package" value="org.devgeeks.Canvas2ImagePlugin.Canvas2ImagePlugin"/>
</feature>
<gap:plugin name="cordova-plugin-file-transfer" source="npm" />
<gap:plugin name="cordova-plugin-camera" source="npm" />
<gap:plugin name="cordova-plugin-imagepicker" source="npm" />
<gap:plugin name="cordova-plugin-device" source="npm" />
<gap:plugin name="cordova-plugin-device-orientation" source="npm" />
<gap:plugin name="cordova-plugin-file" source="npm" />
<gap:plugin name="cordova-plugin-inappbrowser" version="1.0.0" source="npm" />
<gap:plugin name="cordova-plugin-whitelist" source="npm" version="1.0.0"/>
<!--<gap:plugin name='org.crosswalk.engine' version='1.3.0' source='pgb' />-->

<gap:plugin name="org.devgeeks.canvas2imageplugin" version="0.6.0" />
<gap:plugin name="com-badrit-printplugin" source="npm" />
<gap:plugin name="de.appplant.cordova.plugin.printer" version="0.7.1" />
<gap:plugin name="nl.x-services.plugins.socialsharing" version="4.3.8" />

<!-- Allow whitelist -->
<allow-navigation href="*" />
<allow-intent href="*" />
<access origin="*" />


and every html file contains although I have tested several variations of this.

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline' 'unsafe-eval'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; img-src 'self' data: blob: filesystem:; media-src mediastream:">


Although all the right alert are called including success, a file is never created.

Happy yo answer further questions.
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
Since the file-transfer plugin includes the file plugin, you might want to try without the latter in your config.
Also, you should remove all 'feature' elements, as they are deprecated at PGB.
Photo of Jade George

Jade George

  • 2 Posts
  • 0 Reply Likes
Thanks. I made those changes but the problem remains.
Photo of Siva Ram

Siva Ram

  • 5 Posts
  • 0 Reply Likes
Hi All,

I fixed the issue my self.

I am happy to say this, please find the below code changes.

Don't use cli-5.2.0 version , instead of cli-5.2.0 use cli-5.1.1

find the changes in config.xml

and the update all the cordova plugins to npm like below .



<preference name="phonegap-version" value="cli-5.1.1"/>

<gap:plugin name="cordova-plugin-whitelist" version="1.0.0" source = "npm"/>
<gap:plugin name="cordova-plugin-console" version="1.0.1" source = "npm"/>
<gap:plugin name="cordova-plugin-device-orientation" version="1.0.1" source = "npm"/>
<gap:plugin name="cordova-plugin-globalization" version="1.0.1" source = "npm"/>
<gap:plugin name="cordova-plugin-inappbrowser" version="1.0.1" source = "npm"/>
<gap:plugin name="cordova-plugin-network-information" version="1.0.1" source = "npm"/>
<gap:plugin name="cordova-plugin-device" version="1.0.1" source = "npm"/>
<gap:plugin name="cordova-plugin-file-transfer" source="npm" version="1.3.0 " />



and

inside onDeviceReady() function put this code.


function onSuccess(fileSystem) {
// methods/properties for file path:
// fileSystem.name, fileSystem.root.toURL(), fileSystem.root.toInternalURL(), fileSystem.root.nativeURL

gFileSystemVar = fileSystem;
if(device.platform === 'iOS'){
gPersistantPath = fileSystem.root.toInternalURL();
}
else{
gPersistantPath = cordova.file.externalDataDirectory;
}
}
function onError(fileSystem) {
// Error ocurred while calling requestFileSystem.
// console.log("Error in accessing requestFileSystem" + fileSystem.name);
}
// request the persistent file system
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccess, onError);


The same code is working for me and i can able to download the file in to directory.

and find below function to download the file in to directory.



var fileTransfer = new FileTransfer();
var uri = encodeURI("http://some.server.com/download.php");

fileTransfer.download(
uri,
fileURL,
function(entry) {
console.log("download complete: " + entry.toURL());
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
},
);



For any clarifications, give reply to this thread.
Photo of Willy Quesada

Willy Quesada

  • 5 Posts
  • 0 Reply Likes
I'll try this, I'll let you guys know how it goes.
Photo of JesseMonroy650 (Volunteer)

JesseMonroy650 (Volunteer), Champion

  • 3325 Posts
  • 122 Reply Likes
Nice work Siva!
Photo of Siva Ram

Siva Ram

  • 5 Posts
  • 0 Reply Likes
Thank you !! @ Jesse.

let me know guys ,if still facing any issues.

and

var fileURL = gPersistantPath + "/" + fileName + "." + fileFormat;
Photo of Fabio Andres Pino Gutierrez

Fabio Andres Pino Gutierrez

  • 2 Posts
  • 0 Reply Likes
i have the same problem, with phonegap-cli v 5.3.9, i will test to downgrade to 5.1.1
Photo of Fabio Andres Pino Gutierrez

Fabio Andres Pino Gutierrez

  • 2 Posts
  • 0 Reply Likes
already post the issue of this problem at git hub of phonegap-cli if somebady want follow: https://github.com/phonegap/phonegap-...