I am trying to upload a from on my VueJS client to my Flask Server. I am sending the image in a FormData which includes other form fields. I am able to access the other form fields. However, whatever that is send to my server does not seem to be able to be be saved in my flask app. If it helps the data is being sent to my api as a immutable multi-dictionary.
Vuejs
<template>
<ValidationObserver v-slot="{ handleSubmit }">
<b-form @submit.prevent="handleSubmit(onSubmit)" enctype="multipart/form-data">
<ValidationProvider rules="required" v-slot="{ errors }">
<b-form-group
id="price-group"
label="Price:"
label-for="price-input"
>
<b-form-input
id="price-input"
v-model="productForm.price"
type="text"
required
placeholder="Enter price of the product..."
></b-form-input>
</b-form-group>
<span>{{ errors[0] }}</span>
</ValidationProvider>
<b-button-group>
<b-button
type="submit" variant="primary">Submit</b-button>
</b-button-group>
</template>
<script>
import axios from 'axios';
export default {
data() {
return {
productForm: {
price: '',
image_set: {
coverphoto: null,
},
showMessage: false,
notification: '',
},
coverphotoURl: '',
};
},
methods: {
createProduct(payload) {
const path = 'http://127.0.0.1:5000/new_products';
axios.post(path, payload)
.then(() => {
this.showMessage = true;
this.notification = 'Registration Successful';
})
.catch(() => {
this.showMessage = true;
this.notification = payload;
});
},
initForm() {
this.productForm.price = '';
this.productForm.image_set.coverphoto = null;
},
onSubmit() {
const payload = new FormData();
payload.append('price', this.productForm.price);
payload.append('coverphoto', this.productForm.image_set.coverphoto, this.productForm.image_set.coverphoto.name);
this.createProduct(payload);
},
onFileChange(e) {
const file = e.target.files[0];
this.productForm.image_set.coverphoto = file;
this.coverphotoURl = URL.createObjectURL(file);
},
</script>
Flask File
@app.route('/new_products', methods=['POST'])
def new_products():
if request.method == 'POST':
data = request.form
if request.files:
images = request.files.getlist('coverphoto')
else:
return jsonify({'message' : 'No File Uploaded'})
print(images) # Print images will return [<FileStorage: 'Coffee Roasters.jpg' ('image/jpeg')>]
newProduct = Products(str(uuid.uuid4()), data['name'], data['tasting_notes'], data['origination'], data['pairing'], data['price'])
try:
db.session.add(newProduct)
db.session.commit()
filename = secure_filename('coverphoto.jpg')
img_path = os.path.join('/',app.config['UPLOAD_FOLDER'], filename)
images.save(img_path)
new_img = Images(str(uuid.uuid4()), img_path, newProduct.id)
db.session.add(new_img)
db.session.commit()
return jsonify({'message' : 'Upload Successful'})
except:
return jsonify({'message' : 'Upload Unsuccessful'})
It how can i convert [<FileStorage: 'Coffee Roasters.jpg' ('image/jpeg')>] to something that flask is able to save? Thank you for yur help.