在ajax调用完成后开始下载文件 [英] Begin downloading file after ajax call completed

查看:76
本文介绍了在ajax调用完成后开始下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$.ajax({
                    type: "POST",
                    url: "processform.php",
                    dataType: "json",
                    data: {name: name, email: email, city: city, country: country, day: day, month: month, year: year}
                    }).done(function(msg){

                        if(msg.success == 1){
                            $("#success_msg").append('<p>'+msg.message+'</p>');
                            window.location.href = './music/song.mp3';
                        }

                    });

上面的代码只是加载了一个带有音乐播放器的新页面。我希望它像文件一样下载。

The code above just loads a new page with a music player. I want it to download like a file.

推荐答案

你可以通过PHP来创建一个带有正确标题的PHP文件重定向到该页面,它将强制浏览器下载文件。

You could do it via PHP, create a PHP file with the correct headers and than redirect to that page, it will force the browser to download the file.

<?php
header('Content-disposition: attachment; filename=song.mp3');
header('Content-type: audio/mpeg3');
readfile('song.mp3');
?>

这篇关于在ajax调用完成后开始下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆