我正在尝试发送一张表格和一张图片。 注意:我不想将图像保存在数据库中,我想将它保存在我在服务器上创建的文件夹中,然后将图像链接添加到数据库中。
从服务器端我知道如何处理这个,但我不知道如何从字体端执行此操作。换句话说,如何使用 axios 将图像发送到服务器。
<template>
<input type="text" class="form-control" id="specdesc" v-model="product.specdesc" name="specdesc" placeholder="Enter your name">
<input type="file" name="image" id="image" accept="image/*" >
<button type="submit" class="btn btn-sm btn-primary"@click.prevent="Submit()"> Submit</button>
</template>
<script>
export default {
name: 'Addproduct',
data(){
return{
image: '',
product:{
specdesc:'',
},
}
},
methods:{
Submit(){
var _this=this
// console.log(this.image)
console.log(this.selectedCategory.category_name)
var product = {
specification:this.product.specdesc,
imagename:this.image
}
this.$http.post('http://localhost:3000/api/companyproducts',product)
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log("error.response");
});
}
},
}
</script>
现在我的问题是如何使用 axios 上传图像以及输入名称。此外,我想使用相同的方法即 var product
发送。
原文由 Rocky 发布,翻译遵循 CC BY-SA 4.0 许可协议
一个标准的(大部分)方法是将逻辑一分为二,如果你想将图像路径保存在你的
product
,你首先需要将照片上传到服务器并返回它们的路径。伪例子:
组件的数据