强制下载.mp3或.zip文件的Ajax成功 [英] Force Download of .mp3 or .zip files on Ajax success
问题描述
在用户填写电子邮件验证表(通过AJAX提交)后,如果AJAX通话响应成功,我想强制下载到当前浏览器窗口中。
After a user fills out an email verification form (which is submitted via AJAX), I want to force a download to start in the current browser window if the AJAX call response is successful.
电子邮件验证表单如下:
The email verification form looks like:
<form id="form_download_email" method="POST" action="music_email_verification.php">
<input id="download_email" type="text" tabindex="1" /> <br/>
<a href="javascript:void(0);" id="download_email_button">
Download!
</a>
</form>
一旦点击download_email_button,js / jquery就像这样:
The js/jquery once the "download_email_button" is clicked looks like this:
$("#download_email_button").click(function() {
var action = $("#form_download_email").attr('action');
var form_data = {email: $("#download_email").val()};
$.ajax({
type: "POST",
url: action,
data: form_data,
success: function(response){
if(response == 'success'){
window.location.assign('URL to .mp3 or ZIP');
}
}
});
});
输入响应==成功块,现在是我发现强制的唯一解决方案下载一个文件而不启动弹出窗口(通过window.open - 被Chrome阻止),是使用window.location.assign如上所示。这是正确的方法来强制通过JS的文件下载吗?
The response=='success' block gets entered, and for now the only solution I found to force a download of a file without launching a pop up window (through window.open - which gets blocked by Chrome anyway), is to use window.location.assign as shown above. Is this the correct way of going about forcing a file download through JS??
但是,使用window.location.assign方法我只能下载1个文件,如果我尝试用两个mp3插入两行,但不行。 AKA:
However, using the window.location.assign method I can only download 1 file, and if I try to put in two lines with two mp3s, it doesn't work. AKA:
window.location.assign('http://song1.mp3');
window.location.assign('http://song2.mp3');
我不希望用户在提交表单以下载后必须点击另一个链接.mp3 / .zip,我想尝试在JS / jQuery中做所有这些。
I do not want the user to have to click on another link after submitting the form to have to download the .mp3/.zip, I want to try to do it all in JS/jQuery.
我想到的最终方法是响应==成功浏览器点击文件上的href指向mp3的两个隐藏标签。但是,似乎问题是您无法通过点击触发链接的默认操作,而只是onclick属性。
The final method I thought about was on response=='success' to have the browser click on two hidden tags on the document that had their href pointing to the mp3s. However, it seems that "The issue is that you can't trigger a link's default action via a click" with jquery, just the onclick attribute.
我应该做什么这与js?如果没有办法这样做,我应该尝试像PHP这样的东西吗?
Should I be doing this with js? If there is no way to do this, should I try something like PHP?
有关我现在的方式的任何建议或如何下载多个mp3文件的建议有帮助!
Any suggestions on the current way I do it or recommendations on how to download multiple mp3 files would be helpful!
推荐答案
为每个url创建一个iframe,下载并将iframe的源文件设置为文件url:)
create an iframe for each url to download and set the iframe's source to files url :)
var
urls = ['url_1.mp3', 'url_2.mp3', /* ... , */ 'url_N.mp3'],
doLoad = function(url){
$("<iframe />")
.css("display", "none")
.bind("load", function(e){
this.src == url && $(this).remove();
})
.attr("src", url)
.appendTo($(document.body));
};
for( var i=0; i < urls.length; i++ ) {
doLoad( urls[i] );
}
这篇关于强制下载.mp3或.zip文件的Ajax成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!