【问题标题】:How to send audio wav file generated at the server to client browser? [closed]如何将服务器生成的音频 wav 文件发送到客户端浏览器? [关闭]
【发布时间】:2013-06-26 06:16:22
【问题描述】:

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

知道怎么做吗?

【问题讨论】:

    标签: python html web flask


    【解决方案1】:

    你可以用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")
    

    【讨论】:

    • 请告诉如何将wav文件传递给您的代码?
    • 不是将文件保存在磁盘上,而是将其保存到buf
    • 假设我的磁盘中有文件,我想将它发送到客户端,然后我需要提及 wav 文件路径名。
    • 我以为你想生成一个 wav 并发送它而不将其保存到磁盘。
    • 那么你可以使用send_fileflask.pocoo.org/docs/api/#flask.send_file
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-06
    • 2016-02-17
    • 1970-01-01
    相关资源
    最近更新 更多