vue+elementui 上传封面(本地图片)到后台

重点代码

<el-upload
  action=""
  list-type="picture-card"
  :on-change="aa"                    //重点代码
  :auto-upload="true"				//false是默认不上传--这个地方需要改成true,
  :limit="1">						//1代表只能上传1张图片
    <i slot="default" class="el-icon-plus"></i>
    <div slot="file" slot-scope="{file}">
      <img
        class="el-upload-list__item-thumbnail"
        :src="file.url" alt=""
      >
      <span class="el-upload-list__item-actions">
        <span
          class="el-upload-list__item-preview"
          @click="handlePictureCardPreview(file)"
        >
          <i class="el-icon-zoom-in"></i>
        </span>
        <span
          v-if="!disabled"
          class="el-upload-list__item-delete"
          @click="handleDownload(file)"
        >
          <i class="el-icon-download"></i>
        </span>
        <span
          v-if="!disabled"
          class="el-upload-list__item-delete"
          @click="handleRemove(file)"
        >
          <i class="el-icon-delete"></i>
        </span>
      </span>
    </div>
</el-upload>
<el-dialog :visible.sync="dialogVisible">
  <img width="100%" :src="dialogImageUrl" alt="">
</el-dialog>
<script>
  export default {
    data() {
      return {
        dialogImageUrl: '',
        dialogVisible: false,
        disabled: false,
        pit: new FormData(),				//重点代码
      };
    },
    methods: {
	    aa(file) {							//重点代码aa(file)
	      this.pit.append("articleCover", file.raw);
	    },
	    
	    Submit(){					//重点代码(点击提交的按钮)
		  this.pit.append("xxx", this.xxx); //传其他参数-
	      this.pit.append("xxx", this.xxx); //传其他参数
	      ....等等等
	       this.$axios({
	        method: "post",
	        url: "Articlemessage/insertArticlemessage",			//后台给我的接口
	        headers: {
	          "Content-Type": "multipart/form-data",
	        },
	        data: this.pit,
	      })
	        .then((res) => {
	          console.log("res.data", res.data);
	          if (res.data.status == 200) {
	            this.$message({
	              message: res.data.msg,
	              type: "success",
	            });
	          }
	        })
	        .catch((err) => {
	          console.log("error", err);
	        });
	    },
      handleRemove(file) {
        console.log(file);
      },
      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      },
      handleDownload(file) {
        console.log(file);
      }
    }
  }
</script>

《前腾讯员工教我的方法》

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 中使用 ElementUI 进行图片上传,可以通过以下步骤来实现: 1. 首先,确保已经安装了 Vue.js 和 ElementUI。 2. 创建一个 Vue 组件,用于处理图片上传功能。可以命名为 ImageUpload.vue。 3. 在 ImageUpload.vue 组件中引入 ElementUI上传组件,代码如下: ```html <template> <div> <el-upload action="/api/upload" :on-success="handleSuccess" :before-upload="handleBeforeUpload" :show-file-list="false" > <el-button type="primary" icon="el-icon-upload">点击上传图片</el-button> </el-upload> </div> </template> <script> export default { methods: { handleSuccess(response) { // 上传成功后的处理逻辑 console.log(response); }, handleBeforeUpload(file) { // 在上传之前的处理逻辑,比如限制图片类型和大小 const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'; const isLt2M = file.size / 1024 / 1024 < 2; if (!isJPG) { this.$message.error('上传图片只能是 JPG/PNG 格式!'); } if (!isLt2M) { this.$message.error('上传图片大小不能超过 2MB!'); } return isJPG && isLt2M; }, }, }; </script> ``` 4. 在上述代码中,`<el-upload>` 组件是 ElementUI 提供的上传组件。其中,`action` 属性指定了上传的 URL 地址,在这里可以根据需求进行修改;`on-success` 属性绑定了上传成功后的处理函数,可以在其中进行需要的处理;`before-upload` 属性绑定了上传之前的处理函数,可以在其中进行图片类型和大小的限制等处理;`show-file-list` 属性设置为 `false`,表示不显示已上传文件的列表。 5. 可以在 `<el-upload>` 标签内部添加其他元素和样式来进行页面布局和美化,以满足自己的需求。 6. 最后,将 ImageUpload.vue 组件引入到需要使用图片上传功能的页面中,并在相应的位置使用组件标签,即可实现图片上传功能。 以上就是使用 VueElementUI 进行图片上传的简单实现方式。根据实际需求,可以根据 ElementUI 上传组件的 API 进行更多的自定义和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值