I would like to write a service for converting files from one format to another (something similar at https://convertio.co/mp4-gif/) . I already have a file upload button that allows the user to upload a file from their device to the site. There is a box that displays the file name, size, desired format for conversion and a button to convert. By pressing the convert button (ConvertFileButton) a request is sent to the backend. And now I have a question: how can I get an already converted file from the backend and display it for the user (so that the user can download a new converted file)
Now in order:
below is the "CONVERT" button component that sends a request to the backend
export default function ConvertFileButton() {
const { file } = useContext(AppContext)
const formData = new FormData();
formData.append('file', file);
return (
<MyButton onClick={() => { fetch('http://localhost:5000/transcript',
{ method: 'post', body: formData }); }}>CONVERT</MyButton>
)
}
Below I will present three functions on the backend that process the request. The backend is written in Python using Flask.
The first function to which the file is sent and in which the conversion takes place
@mod.route('/transcript', methods=['POST', "GET"])
def create_transcript_task():
return redirect(url_for('backend.status_page') + f'?id={task_id}')
then you can get the finished converted file
@mod.route('/get', methods=['GET'])
def get_result():
return send_file(file_path)
Thus, I will explain the question: how can I display this finished file again in React, so that the user can download a new converted file.
As I understand it, you need to create a component in which to transfer this new file. But I don't understand how to do it. Perhaps you can help me
export default function DisplayConvertedFile() {
return (
// some code //
)
}