Change name of download in javascript

前端 未结 3 1116
失恋的感觉
失恋的感觉 2020-11-27 07:08

If a user clicks on a downloadable link such as

Download

Is there a client-side (html or javas

相关标签:
3条回答
  • 2020-11-27 07:23

    HTML5 provides the a[download] attribute which lets you rename a file. This example will download link.txt and rename it something.txt.

    ​<a download="something.txt" href="link.txt">asdf</a>​​​​​​​​​​​​​​​​​​​​​​​​​​​
    

    Note that this only works on same-origin URLs (i.e. not across different domains).

    0 讨论(0)
  • 2020-11-27 07:27

    No, you cannot change this from the client side (HTML or javascript). You need to change it from the server. One way is to use a server side script which will set the Content-Disposition HTTP response header:

    Content-Disposition: attachment; filename=somecustomname.txt
    
    0 讨论(0)
  • 2020-11-27 07:30

    You can use Filesaver.js script written by eligrey(Im using angularjs in the example here) You can achieve the same in classic javascript using XmlHttpRequest object

    //In your html code , add these : ->
    <script src="https://rawgit.com/eligrey/FileSaver.js/master/FileSaver.js" type="text/javascript"></script>
     <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular-animate.js"></script>
    //In your Javascript:- 
    
    $http({
            url: "url where the file is located",
            method: "GET",
            responseType: "blob"
        }).then(function (response) {
    
    saveAs(response.data,"newfilename.extension");
    
    })
    
    0 讨论(0)
提交回复
热议问题