简体   繁体   English

在浏览器中下载文件

[英]Download a file in the browser

On my web page I have links for downloading mp3 files. 在我的网页上,我有用于下载mp3文件的链接。

Upon user click, I make an ajax call and create an mp3 file on the server. 用户单击后,我会进行ajax调用并在服务器上创建一个mp3文件。

I return the path of the file to the script but now, how do I make it download the user system? 我将文件的路径返回到脚本,但是现在,如何使它下载用户系统?

<SCRIPT TYPE="text/javascript">
    function voice(id){
        $.ajax({
                url:'/download/',
                type:"POST",
                data:{'id':id,'csrfmiddlewaretoken':$('input[name=csrfmiddlewaretoken]').val()},
                success:function(return_data) {
                    alert(return_data['url']);
                },
                error: function(){
                    alert("Some Error");
                }
            });
    }
</SCRIPT>

I get the url of the mp3 file in alert but how to download it ? 我在警报中得到了mp3文件的网址,但如何下载呢?

Thank you. 谢谢。

Create an anchortag in your HTML which is hidden using CSS 在HTML中创建一个使用CSS隐藏的锚标签

 <a href="#" id="someID" class"hiddenUrl" style="visibility: hidden" target="_blank">Hidden</a>

And in your javascript 在你的javascript中

<SCRIPT TYPE="text/javascript">
    function voice(id){
        $.ajax({
                url:'/download/',
                type:"POST",
                data:{'id':id,'csrfmiddlewaretoken':$('input[name=csrfmiddlewaretoken]').val()},
                success:function(return_data) {
                  var url= return_data['url'];
                   $('.hiddenUrl').attr('href',url) //adding value to the href attribute
                    $('.hiddenUrl').attr('download','any_filename.mp3');
                     $('.hiddenUrl')[0].click();

                },
                error: function(){
                    alert("Some Error");
                }
            });
    }
    </SCRIPT>

You can do window.location = return_data['url']; 您可以执行window.location = return_data['url']; assuming that you have an url which starts with http... This approach is equivalent of the user clicking the link to the mp3. 假设您有一个以http开头的网址...这种方法等效于用户单击mp3的链接。 Another approach would be to create an iframe with src set to the newly created link. 另一种方法是创建一个src设置为新创建的链接的iframe。 Using this method the user's browser will prompt to download the file without having to change the location (navigating away from the current page). 使用此方法,用户的浏览器将提示您下载文件,而无需更改位置(从当前页面导航)。 I recommend the first approach. 我建议第一种方法。

This should work: 这应该工作:

<SCRIPT TYPE="text/javascript">
    function voice(id){
        $.ajax({
            url:'/download/',
            type:"POST",
            data:{'id':id,'csrfmiddlewaretoken':$('input[name=csrfmiddlewaretoken]').val()},
            success:function(return_data) {
                window.location.href = return_data['url'];
            },
            error: function(){
                alert("Some Error");
            }
        });
}
</SCRIPT>
I think this will work
 <SCRIPT TYPE="text/javascript">
        function voice(id){
            $.ajax({
                url:'/download/',
                type:"POST",
                data:{'id':id,'csrfmiddlewaretoken':$('input[name=csrfmiddlewaretoken]').val()},
                success:function(return_data) {
                    var url= return_data['url'];
                    window.location.assign(url);
                },
                error: function(){
                    alert("Some Error");
                }
            });
    }
    </SCRIPT>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM