![](/img/trans.png)
[英]How to allow users to download an excel file using python/Flask?
[英]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.