简体   繁体   中英

Send file as a response from ajax request

I have a javascript function that sends the name of the audio file on my server to a php script using jquery get. It looks like this:

function getAudioClip() {
    var x = document.getElementById("clips").value;
    $.get( "get_audio.php", { filename: x } )
      .done(function( data ) {
        loadAudioClip(data);
      });
}

The function loadAudioClip(data) displays the waveform of the audio file given as data. I want to make get_audio.php return the audio file with the file name x, so I can use it to display the waveform using my loadAudioClip(data) function.

Edit: I figured out that maybe I can get a file directly instead of using php, like this

function getAudioClip() {
    var x = document.getElementById("clips").value;
    $.get( "audio/" + x , function( data ) {
      loadAudioClip(data);
    });
}

Is that possible? x is a filename(ex. MyRecording02.wav).

Opening an audio file with PHP will be slow process. Maybe you can check the existence of the file with PHP, and then if file exists, simply do something like this:

header("Location: $filename");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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