如何將檔案從 Flask 發送到 ReactJS?
我已經在前端撰寫了代碼,用戶上傳一個檔案,然后該檔案進入 Flask 服務器,然后在 Flask 服務器中修改檔案,但我被困在那里......
下一步是我想發送燒瓶服務器中的“修改檔案”并在前端 ReactJS 中接收它,以便用戶可以下載“修改檔案”
例如,這是修改檔案燒瓶功能。
#modify the file
@app.route("/convertor", methods=['POST'])
def convertor():
files = request.files
#file = files.get('file').read()
file_name = files.get('file')
print(file_name.filename)
# Convert img to pdf
img1 = Image.open(file_name)
img = img1.convert('RGB')
img.save(os.path.abspath(f'Backend/{file_name.filename}.pdf'))
我知道它需要回傳一些東西,我怎樣才能回傳檔案并在 ReactJS 中接收它然后下載它?
任何的想法?
uj5u.com熱心網友回復:
您將不得不在單獨的導演中臨時下載該上傳的檔案,然后
- 將該檔案的 url 回傳到您的 React 前端
- 創建一個新函式以從臨時目錄中洗掉該檔案
@app.route("/remove_file/<file_id>", methods=['DELETE'])
def remove_file(file_id):
if (os.path.isFile('your_temp_directory/' file_id)):
os.remove('your_temp_directory/' file_id)
編輯:您可以使用Wget python 包從上傳的檔案內容中下載
wget [options eg. multiple files, download speed etc.] -0 dir/path file_url
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/406599.html
標籤:
