How to send a pdf file from Flask to ReactJS

2024/11/16 19:36:16

How can I send a file from Flask to 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...

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"

For example, this is the modify file 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 pdfimg1 = 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?

Any idea?

Answer

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
  2. create a new function to remove that file from the temp directory
@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)

edit: you can download from the uploaded file content by using the Wget python package

wget [options eg. multiple files, download speed etc.] -0 dir/path file_url
https://en.xdnf.cn/q/119159.html

Related Q&A

How to draw cover on each tile in memory in pygame

I am a beginner in pygame and I am not a English native speaker.My assignment is coding a game called Memory. This game contains 8 pairs pictures and an cover exists on each pictures. This week, our as…

Compare 2 Excel files and output an Excel file with differences

Assume for simplicity that the data files look like this, sorted on ID:ID | Data1 | Data2 | Data3 | Data4 199 | Tim | 55 | work | $55 345 | Joe | 45 | work | $34 356 | Sam |…

Problem to show data dynamically table in python flask

I want to show a table in the html using python flask framework. I have two array. One for column heading and another for data record. The length of the column heading and data record are dynamic. I ca…

RuntimeError: generator raised StopIteration

I am in a course and try to find my problem. I cant understand why if I enter something other than 9 digits, the if should raise the StopIteration and then I want it to go to except and print it out. W…

split list elements into sub-elements in pandas dataframe

I have a dataframe as:-Filtered_data[defence possessed russia china,factors driving china modernise] [force bolster pentagon,strike capabilities pentagon congress detailing china] [missiles warheads, d…

Image does not display on Pyqt [duplicate]

This question already has an answer here:Why Icon and images are not shown when I execute Python QT5 code?(1 answer)Closed 2 years ago.I am using Pyqt5, python3.9, and windows 11. I am trying to add a…

Expand the following dictionary into following list

how to generate the following list from the following dictionary d = {2: 4, 3: 1, 5: 3}f = [2**1,2**2, 2**3, 2**4, 3**1, 5**1, 5**2, 5**3, 2**1 * 3, 2**2 * 3, 2**3 * 3, 2**4 * 3, 5**1 * 3, 5**2 * 3, 5*…

how can I improve the accuracy rate of the below trained model using CNN

I have trained a model using python detect the colors of the gemstone and have built a CNN.Herewith Iam attaching the code of mine.(Referred https://www.kaggle.com) import os import matplotlib.pyplot a…

Classes and methods, with lists in Python

I have two classes, called "Pussa" and "Cat". The Pussa has an int atribute idPussa, and the Cat class has two atributes, a list of "Pussa" and an int catNum. Every class …

polars dataframe TypeError: must be real number, not str

so bascially i changed panda.frame to polars.frame for better speed in yolov5 but when i run the code, it works fine till some point (i dont exactly know when error occurs) and it gives me TypeError: m…