图片上传 axios

     <div v-if="photoShow">
             图片上传:
             <input type="file" placeholder="商品图片" class="marleft marright clearmoren" id="chuan" ref="search">
             <el-button type="primary" round size="mini" @click="upload">上传</el-button>
      </div>
            //上传图片
            upload: function () { 
                var f = this.$ref.photo;
                console.dir(f.files[0]);
                let param = new FormData();
                param.append('img', f.files[0]);//通过append向form对象添加数据
                console.log(param.get('img')); //FormData私有类对象,访问不到,可以通过get判断值是否传进去
                let config = {
                    headers: { 'Content-Type': 'multipart/form-data' }
                };  //添加请求头
                axios.post(this.$store.state.https + this.$store.state.geturl.upload + '?token=' + this.$store.state.tokenstatic, param, config)
                    .then(response => {
                        this.form.url = this.$store.state.geturl.photourl + response.data.data.url;
                        console.log(this.form.url)
                        if (this.form.url) {
                            alert('图片上传成功')
                        }
                    })
            },


猜你喜欢

转载自blog.csdn.net/zhumizhumi/article/details/79536452