Reputation: 9049
$.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.
Upvotes: 3
Views: 5904
Reputation:
If you give up automating the download after the ajax call, You can do this. The browsers will handle this situation in their own way.
$.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';
$('<a/>', {target:'_blank', href:'./music/song.mp3'}).appendTo($("#success_msg")).html('Click To Download <Filename>');
}
});
Upvotes: 2
Reputation: 2780
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');
?>
Upvotes: 4
Reputation: 13248
try to do this way:
header("Content-Disposition: attachment; filename=somefile.mp3;");
Upvotes: 3
Reputation: 29739
That's browser dependent. If your browser has a media plugin it might open media files directly in the browser instead of offering a download dialog.
Upvotes: 0