How can I get the filename from a given URL
in PhoneGap?
In JavaScript, I used something like this:
var uri = encodeURI("");
My application downloads the file, but I have to manually set the file name. To illustrate, when I call
onclick="descarga('')"
this function is run:
function descarga(URL){
var rutaarchivo = "file:///sdcard/data/.protocolo/test1.pdf";
alert(rutaarchivo);
var filetransfer = new FileTransfer();
filetransfer.download(URL, rutaarchivo,
function(entry){
alert("Download plete : " + entry.fullPath);
},function(error) {
alert("download error source " + error.source);
});
}
This saves the download to ../.protocolo
, and its filename is test.pdf
. I want to be able to save it as the name it is set as on the server (*manual_openerp.230209.pdf*) at the real URL, *...//www.openerpspain/descargas/manual_openerp.230209.pdf*.
How do I do that?
How can I get the filename from a given URL
in PhoneGap?
In JavaScript, I used something like this:
var uri = encodeURI("http://www.openerpspain./descargas-documentacion?download=2");
My application downloads the file, but I have to manually set the file name. To illustrate, when I call
onclick="descarga('http://www.openerpspain./descargas-documentacion?download=2')"
this function is run:
function descarga(URL){
var rutaarchivo = "file:///sdcard/data/.protocolo/test1.pdf";
alert(rutaarchivo);
var filetransfer = new FileTransfer();
filetransfer.download(URL, rutaarchivo,
function(entry){
alert("Download plete : " + entry.fullPath);
},function(error) {
alert("download error source " + error.source);
});
}
This saves the download to ../.protocolo
, and its filename is test.pdf
. I want to be able to save it as the name it is set as on the server (*manual_openerp.230209.pdf*) at the real URL, *...//www.openerpspain./descargas/manual_openerp.230209.pdf*.
How do I do that?
Share Improve this question edited Oct 27, 2013 at 10:29 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Apr 11, 2013 at 10:33 BurasukoBurasuko 191 silver badge4 bronze badges4 Answers
Reset to default 2String fileName = url.substring(url.lastIndexOf('/') + 1);
where url
is the plete path of your file.
Use:
url="file:///sdcard/data/.protocolo/test1.pdf?getVar=value";
url.replace(/\?.*$/,"").replace(/.*\//,"");
This is basically asking How to get the file name from a full path using JavaScript?. Here, the solution was:
var filename = url.replace(/^.*[\\\/]/, '');
I added in my URLs parameters. Then in PhoneGap I used:
function getParameterByName( name,href ){
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( href );
if( results == null )
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
Called from :
var filename = getParameterByName ("nombrefichero", url);
So this way I can get example.pdf from:
http://www.download./fake/example.pdf?nombrefichero=example.pdf
And then I can download it, and save it where I want.