简体   繁体   English

发送文件作为来自ajax请求的响应

[英]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. 我有一个javascript函数,可使用jquery get将服务器上音频文件的名称发送到php脚本。 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. 函数loadAudioClip(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. 我想让get_audio.php返回文件名为x的音频文件,因此我可以使用loadAudioClip(data)函数使用它来显示波形。

Edit: I figured out that maybe I can get a file directly instead of using php, like this 编辑:我发现也许我可以直接获取文件而不是使用php,像这样

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). x是文件名(例如MyRecording02.wav)。

Opening an audio file with PHP will be slow process. 用PHP打开音频文件的过程很慢。 Maybe you can check the existence of the file with PHP, and then if file exists, simply do something like this: 也许您可以使用PHP检查文件是否存在,然后如果文件存在,只需执行以下操作:

header("Location: $filename");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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