element ui組件方法的傳遞 //引入 組件。 <el-upload class="avatar-uploader" :action="action" :http-request="uploadFile" :show-file-list="false"> <img v-if="imageUrl" :src="imageUrl" class="avatar"> <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload> uploadFile(data){ var userinfo = this.$baseMethod.getStore('_ccmUser') let formdata = new FormData(); formdata.append('file',data.file); formdata.append('token',userinfo.token); formdata.append('op',"uploadChannelPic"); formdata.append('data',JSON.stringify({})); uploadFile(formdata).then(res => { this.imageUrl = JSON.parse(res.data).picUrl // console.log(JSON.parse(res.data)) }) },
這一段是js上傳FormData請求格式app
<input type="file" id="file" //設置上傳格式化 accept="image/jpg,image/jpeg,image/png,image/gif" //獲取上傳內容 @change="fileUpload()" /> fileUpload() { var f = document.getElementById('file').files[0]; let formdata = new FormData() formdata.append('file',f); formdata.append('token',storage.get('loginToken')); formdata.append('op',"uploadChannelPic"); formdata.append('data',JSON.stringify({})); //這裏是請求接口進行傳遞formdata this.$upload("/channel/perfection/uploadFile",formdata ).then(res => {
//請求成功後賦值給要展現的圖片 this.fileImg = JSON.parse(res.data).picUrl }) },