Adam
Adam

Reputation: 9049

Begin downloading file after ajax call completed

$.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

Answers (4)

user405398
user405398

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

Erik Terwan
Erik Terwan

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

coder
coder

Reputation: 13248

try to do this way:

header("Content-Disposition: attachment; filename=somefile.mp3;");

Upvotes: 3

Alp
Alp

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

Related Questions