简体   繁体   English

如何将 pdf 文件从 Flask 发送到 ReactJS

[英]How to send a pdf file from Flask to ReactJS

How can I send a file from Flask to ReactJS?如何将文件从 Flask 发送到 ReactJS?

I have already code that in the frontend, the user upload a file and then that file goes to the Flask server, then in the flask server the file is modify, but I'm stuck there...我已经在前端编写了代码,用户上传了一个文件,然后该文件转到 Flask 服务器,然后在 flask 服务器中,文件被修改,但我被困在那里......

The next step is that I want to send that "modify file" that is in the flask server and receive it in the frontend ReactJS so that the user can download the "modify file"下一步是我想发送 flask 服务器中的“修改文件”并在前端 ReactJS 中接收它,以便用户可以下载“修改文件”

For example, this is the modify file flask function.例如,这是修改文件 flask function。

#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'))

I know it needs to return something, how can I return the file and receive it in ReactJS and then download it?我知道它需要返回一些东西,我怎样才能返回文件并在 ReactJS 中接收它然后下载它?

Any idea?任何想法?

you will have to download that uploaded file temporarily in a separate director and then您将不得不在单独的导演中临时下载该上传的文件,然后

  1. return the url of that file to your React frontend将该文件的 url 返回到您的 React 前端
  2. create a new function to remove that file from the temp directory创建一个新的 function 以从临时目录中删除该文件
@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)

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

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