6

我正在为我的应用程序使用烧瓶。我想将音频 wav 文件从服务器端发送到客户端,无论是否将 wav 文件保存在磁盘上。

知道怎么做吗?

4

1 回答 1

10

您可以使用StringIO创建一个内存文件:

from cStringIO import StringIO
from flask import make_response

from somewhere import generate_wav_file  # TODO your code here

@app.route('/path')
def view_method():

    buf = StringIO()

    # generate_wav_file should take a file as parameter and write a wav in it
    generate_wav_file(buf) 

    response = make_response(buf.getvalue())
    buf.close()
    response.headers['Content-Type'] = 'audio/wav'
    response.headers['Content-Disposition'] = 'attachment; filename=sound.wav'
    return response

如果磁盘上有文件:

from flask import send_file

@app.route('/path')
def view_method():
     path_to_file = "/test.wav"

     return send_file(
         path_to_file, 
         mimetype="audio/wav", 
         as_attachment=True, 
         attachment_filename="test.wav")
于 2013-06-28T13:05:09.500 回答