In a web-app that I am developing, users can upload files. As part of the upload process, users must specify what type of file (eg invoice, receipt, contract) it is and also who the customer is.
I then send the file to the backend server using a fetch. From the back-end server, it is to be uploaded to an ftp. On the ftp I need to create a directory based on the file type id and customer id. for example, it should be in the directory ftp/invoices/kfc.
Then, on a database, the server registers the file, its location and, for example, it's upload date.
Ideally I want to send the metadata (type of file, customer) as part of the same fetch.
My backend server is using python and flask.
My frontend code is below. I've tried a couple of things already:
I'm hoping someone can tell me there's an easy way to do this.
The frontend is React-redux, the backend python and flask and the database in MySQL
case 'SEND_FILE_TO_SERVER':
const formData = new FormData();
formData.append('File', action.fileToSend);
fetch(window.location.origin + '/api/submit-file', {
method: 'POST',
body: formData,
})
After some more troubleshooting, I found that the correct approach was indeed to add the metaData to the formData. The reason it had not worked at first was because I was trying to access it wrongly in python flask. So, in javascript you should have
formData.append('File', action.fileToSend);
formData.append('metaData', JSON.stringify(action.metaData));
and then in python flask you should have
@app.route('/api/submit-file', methods=["POST"])
def submit_file():
file = request.files['File']
metaData = loads(request.form['metaData'])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.