Skip to content
Advertisement

Sending a profile picture from react frontend to flask-restful backend and storing

I want to store profile pictures on the file system (images/{username}_pfp.{extension} and store its location in the database as a string. My frontend react code is

JavaScript

and my flask backend code is

JavaScript

I have gotten the bits of code relevant to this from multiple sources (Flask – Get the name of an uploaded file minus the file extension, https://flask.palletsprojects.com/en/2.1.x/patterns/fileuploads/). When the image is sent to the backend, it gives an AttributeError: dict object has no attribute filename in the backend terminal. How can I get this to work? Have I missed something?

Advertisement

Answer

So. I tried some more stuff and I thought that I would reply to this to document it for others. My frontend code ended up with me sending two separate post requests, one to post the json data, and the other for the image.

By the way, frontend code uses react-bootstrap, but the principle is the same. Frontend code:

JavaScript

And the backend code for just the image is

JavaScript

The first post request just defines the profile_picture field as an empty string. Of course, in the user delete function add some lines to delete the profile picture file:

JavaScript

Hope this helps, feel free to comment if you need clarification :). Also, postData and getData functions in comment because they are just fetch requests.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement