繁体   English   中英

如何允许用户使用Flask下载MIDI文件而不下载0字节?

[英]How do I allow users to download a MIDI file with Flask without getting a 0 byte download?

我正在编写一个使用MIDIUtil库创建midi文件的应用程序。 当用户提交HTML表单时,将使用MIDIUtil创建一个midi文件对象。 如何允许用户将其下载为.mid文件? 我尝试了以下代码,但最终下载了0字节的文件。

return Response(myMIDIFile, mimetype='audio/midi')

我使用以下代码的变体来允许我的用户下载他们生成的图像。 下面的代码应该为您工作。 请注意,您很可能需要指定要下载文件的完整服务器路径。

from flask import send_file

download_filename = FULL_PATH_TO_YOUR_MIDI_FILE

return(send_file(filename_or_fp = download_filename,mimetype="audio/midi",as_attachment=True))

我最终使用了它,并且有效。

new_file = open('test.mid', 'wb')
myMIDI.writeFile(new_file)
new_file.close()
new_file = open('test.mid', 'rb')
return send_file(new_file, mimetype='audio/midi')

可能只想尝试使用send_file

from flask import send_file

return send_file("yourmidifile.mid", as_attachement=True, mimetype="audio\midi")

暂无
暂无

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

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