I can download files with phonegap api. (I use cordova 2.9.1)
But only if I know the url, for example: http://domain.com/url/to/img/moo.png.
But in my server I want to read the binary data of the file and send the binary string to
the phonegap app.
Is this possible to achieve this???
Here is the code:
var fileTransfer = new FileTransfer();
// var uri = encodeURI("http://zielekols.com/Bilder/logo.png");
var uri = encodeURI('url/to/get');
fileTransfer.download(
uri,
'/storage/emulated/0/moo.pdf',
function(entry) {
console.log("download complete: " + entry.fullPath);
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
},
false
);
Related
I am downloading a file from the server, it returns in Blob format. I have used the following piece of code to convert blob to URL.
const blob = new Blob([data], { type: 'application/pdf' });
const url= window.URL.createObjectURL(blob);
After that, I have used Cordova-File-Transfer-Plugin code to Download that File. Code provided below:
downloadOverMobile(url){
var fileTransfer = new FileTransfer();
let fileURL = 'cdvfile://localhost/persistent/Download/Invoice.pdf';
var uri = encodeURI(url);
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("download error code " + error.code);
},
false,
{
headers: {
}
}
);
}
I am getting the following response:
download error source blob:file:///d1339351-7d29-4dcd-bba6-0b037003c886
download error target
cdvfile://localhost/persistent/Download/Invoice.pdf
download error code 2
I am using angular 4 and Cordova 8.1.2. Can Anyone tell me what am I missing?
Hello I should save images on the device when the user clicks the download button.
I'm using the cordova file transfer plugin, but I can not save images in the device gallery.
$("#downloadFile").click(function()
{
var fileTransfer = new FileTransfer();
var uri = encodeURI("www.mysite.com/image.jpg");
var fileURL = cordova.file.????;
fileTransfer.download(
uri, fileURL, function(entry)
{
alert("download complete: " + entry.toURL());
},
function(error)
{
alert("download error source " + error.source);
alert("download error target " + error.target);
alert("download error code" + error.code);
},
false,
{
headers:
{
"Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
}
});
});
Are there any other ways to save an image using cordova?
Thanks, ciao
https://www.npmjs.com/package/cordova-plugin-save-image
You should use this plugin
function(entry){
window.cordova.plugins.imagesaver.saveImageToGallery(uri, function(success){}, function(error){});
}
nativePathToJpegImage is uri.
I have a problem with downloading a pdf from an external url and save it on the android device. So far I get these errors:
download error source http://bbg.co.tz/wp-content/uploads/2015/03/CC008962_Tanzania-tax-briefing-transfer-pricing_18-11-15.pdf
index.js:227 download error target /Phone/Download
index.js:228 upload error code1
And here is my code:
function bbgDownload(bbgURL)
{
var fileTransfer = new FileTransfer();
var uri = encodeURI(bbgURL);
fileURL='/Phone/Download'
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);
},
false,
{
headers: {
"Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
}
}
);
console.log("Please show this URL " + bbgURL);
}
Append a filename to your fileURL variable. This worked for me, for example:
var fileTransfer = new FileTransfer();
var uri = encodeURI("http://bbg.co.tz/wp-content/uploads/2015/03/CC008962_Tanzania-tax-briefing-transfer-pricing_18-11-15.pdf");
var fileURL = "cdvfile://localhost/persistent/mydir/myPdfFile.pdf";
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);
},
false,
{
headers: {
"Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
}
}
);
Cordova version: 3.4.1-0.1.0
I am using the file transfer plugin and I cannot save a image to file.
Errors I get from the device:
"download error target err"
upload error code 1
Here is the code, any ideas?
var fileTransfer = new FileTransfer(),
d = new Date(),
testImageUrl = encodeURI('http://www.inutritie.ro/wp-content/uploads/2013/07/otet-600x300.jpg'),
imageCacheDir = '//Testdir';
fileTransfer.download(
testImageUrl,
'file:'+imageCacheDir+'/'+d.getTime(),
function(entry){
alert("download complete: " + entry.fullPath);
},
function(error){
alert("download error source " + error.source);
alert("download error target " + error.target);
alert("upload error code" + error.code);
}
);
Here is how I set and get the directory where I'll save the images on my Android device:
function initLocalCacheDir(){
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onFileSystemFail);
};
function onFileSystemSuccess(fileSystem) {
fileSystem.root.getDirectory("Testdir",{create:true, exclusive: false}, gotDir, gotDirError);
};
function gotDir(d) {
var imageCacheDir = d.fullPath;
};
function gotDirError(evt) {
alert('gotDirError');
};
function onFileSystemFail(evt) {
alert('onFileSystemFail');
};
I am building a android applikation using Phonagap and jquery mobile.
In the app I need to download some mp3-files, but it won't work
Here is my function:
function downloadFile() {
var fileTransfer = new FileTransfer();
var uri = encodeURI("http://media.domain.com/myfile.mp3");
var filepath = remoteFile.substring(remoteFile.lastIndexOf('/')+1);
fileTransfer.download(
uri,
filePath,
function(entry) {
console.log("download complete: " + entry.fullPath);
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
}
);
}
What am I doing wrong..?
remoteFile does not appear to be set.