I have form modal which switches between Edit and Create Post I need to upload a image and submit the form. There is am example to upload an image only field. Here I need to submit the file with other form fields
<form @submit.prevent="editmode ? updatePost() : createPost()">
<div class="modal-body">
<div class="form-group">
<input v-model="form.title" id="title" type="text" name="title"
placeholder="Title"
class="form-control" :class="{ 'is-invalid': form.errors.has('title') }">
<has-error :form="form" field="title"></has-error>
</div>
<div class="form-group">
<input v-model="form.featured" id="featured" type="file" name="featured"
placeholder="Featured Image" @change="selectFile"
class="form-control" :class="{ 'is-invalid': form.errors.has('featured') }">
<has-error :form="form" field="featured"></has-error>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
<button v-show="editmode" type="submit" class="btn btn-success">Update</button>
<button v-show="!editmode" type="submit" class="btn btn-primary">Create</button>
</div>
</form>
Here is the script I am calling to upload image file. I am using this.form.post('api/post') to post the data. How I need to submit multipart/form-data within the script
export default {
data() {
},
methods: {
selectFile(e) {
const file = e.target.files[0];
this.form.featured = file;
},
createPost(){
this.$Progress.start();
this.form.post('api/post')
.then(()=>{
$('#addNew').modal('hide')
this.$Progress.finish();
})
.catch(()=>{
})
}