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
const Signup = () => {
const [state, setState] = useState({
email: "",
password: "",
confirmPassword: "",
username: "",
profile_picture: "",
});
const navigate = useNavigate();
const onSubmit = (e) => {
e.preventDefault();
console.log(state.email, state.password, state.confirmPassword, state.username);
if (state.password === state.confirmPassword) {
getData('http://localhost:5000/users')
.then(data => {
console.log(data);
let userExists = false;
for (let i = 0; i < data.length; i++) {
if (data[i].email === state.email) {
userExists = true;
}
if (data[i].username === state.username) {
userExists = true;
}
}
if (userExists) {
alert("Email or Username already exists");
} else {
const data = new FormData();
for(var x = 0; x<state.profile_picture.length; x++) {
data.append('file', state.profile_picture[x])
}
postData('http://localhost:5000/users', {
email: state.email,
password: state.password,
name: state.username,
profile_picture: data
})
.then(data => {
console.log(data);
alert("User created successfully");
navigate('/');
})
.catch(err => {
console.log(err);
alert("Error creating user");
});
}
})
.catch(err => {
console.log(err);
alert("Error creating user");
});
};
};
return (
<>
<Header />
<div className="container">
<Form>
//creating other values
<Form.Group className="mb-3" controlId="formFile">
<Form.Label>Upload Profile Picture (image format must be png, jpg, or jpeg).</Form.Label>
<Form.Control type="file" onChange={e => setState(prevState => { return {...prevState, profile_picture: e.target.value}})}/>
</Form.Group>
<Button variant="primary" type="submit" onClick={onSubmit}>
Submit
</Button>
</Form>
</div>
</>
);
};
and my flask backend code is
class UserListResource(Resource):
def get(self):
users = User.query.all()
return users_schema.dump(users)
def post(self):
received_file = request.json['profile_picture']
filename = request.json['name'] + '_pfp' + received_file.filename.split('.')[1]
filename = secure_filename(filename)
filename = os.path.join(app.config['UPLOAD_FOLDER'], filename)
received_file.save(filename)
new_user = User(
email=request.json['email'],
password=request.json['password'],
name=request.json['name'],
profile_picture=filename
)
db.session.add(new_user)
db.session.commit()
return user_schema.dump(new_user)
api.add_resource(UserListResource, '/users')
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?