美文网首页
axios 图片上传

axios 图片上传

作者: 快提醒我去学Java | 来源:发表于2018-10-09 14:50 被阅读0次

原文链接: 图片上传 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('图片上传成功')
                    }
                })
        },

相关文章

网友评论

      本文标题:axios 图片上传

      本文链接:https://www.haomeiwen.com/subject/gvneaftx.html