string - Get filename from URL without it in JavaScript - Stack Overflow

How can I get the filename from a given URL in PhoneGap?In JavaScript, I used something like this:var u

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 badges
Add a ment  | 

4 Answers 4

Reset to default 2
String 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745439313a4627757.html

相关推荐

  • string - Get filename from URL without it in JavaScript - Stack Overflow

    How can I get the filename from a given URL in PhoneGap?In JavaScript, I used something like this:var u

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信