admin管理员组文章数量:1430634
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.
本文标签: stringGet filename from URL without it in JavaScriptStack Overflow
版权声明:本文标题:string - Get filename from URL without it in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745439312a2658373.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论