记 Vue3 + Element-Plus 上传组件上传图片到 SM.MS 图床


title: 记 Vue3 + Element-Plus 上传组件上传图片到 SM.MS 图床
date: 2022-05-21 10:10:12
tags:

  • Vue
    categories:
  • 问题记录
    cover: https://cover.png
    feature: false

1. 上传组件

action 表示上传的地址,这里使用 http-request 覆盖默认的上传行为,自定义上传的实现

<template>
  <el-row>
    <el-col :span="12">
      <el-upload
        action="#"
        :http-request="upload"
        :show-file-list="false"
        :before-upload="handleUpload"
      >
        <el-avatar
          :src="$store.state.userInfo.avatar"
          :size="300"
          class="avatar"
        />
        <el-avatar
          :size="300"
          class="mask"
        >
          <el-icon :size="50">
            <Plus />
          </el-icon>
        </el-avatar>
      </el-upload>
    </el-col>
  </el-row>
</template>

<style scoped>
:deep(.el-upload) {
  position: relative;
  border-radius: 50%;
}
.avatar {
  position: absolute;
  z-index: 1;
}
.mask {
  z-index: 5;
  opacity: 0;
  background-color: #0a0a0a;
}
.mask:hover {
  opacity: 0.5;
}

2. 上传实现

创建一个 formData 对象,然后将组件传进来的文件 file 传进去。这里的参数名 smfile 是图床接口所需要的参数,类型为 file

function upload(file) {
      const formData = new FormData();
      formData.append('smfile', file.file);

      axios.post('/api/v2/upload', formData, {
        headers: {
          'Content-Type': 'multipart/form-data',
          'Authorization': 'xxx',
        },
      }).then((res) => {
        console.log(res);
      });
}

设置 headersContent-Typemultipart/form-data,然后设置 Authorization 为 sm:ms 的 API Token

3. 代理设置

注意现在 SM.MS 国内最新的地址为 https://smms.app

这里需要把响应超时时间设长一点,上传速度可能比较慢,我给的是15秒。假如由于 https 等问题上传失败,可以将注释掉的代码放开,secure 表示安全证书校验,设为 false,然后设置 Referer,表示请求来源

const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true,
  devServer: {
    port: '8081',
    proxy: {
      '/fanBlog': {
        target: 'http://localhost:8888',
        ws: true,
        changeOrigin: true
      },
      '/api': {
        target: "https://smms.app",
        ws: true,
        changeOrigin: true,
        // secure: false
        // Headers: {
        //   'Referer': 'https://smms.app'
        // }
      }
    }
  }
})

注意这里的文件类型,一般浏览器会默认转为 multipart/form-data,使用 FormData 来进行传输

可以看到上传成功后返回了响应结果

SM.MS 管理页面也已经能看到上传的图片

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue 3中,可以使用 `v-for` 指令和 `provide/inject` API 来实现父组件校验多个子组件的表单。 首先,在子组件中定义一个表单对象,并且使用 `provide` API将表单对象提供给父组件,例如: ``` <template> <el-form :model="form"> ... </el-form> </template> <script> import { provide } from 'vue'; export default { data() { return { form: { ... } } }, mounted() { provide('form', this.form); } } </script> ``` 然后,在父组件中使用 `v-for` 指令和 `inject` API 获取所有子组件的表单对象,例如: ``` <template> <el-form ref="parentForm"> <child-form v-for="(item, index) in items" :key="index"></child-form> ... </el-form> </template> <script> import { inject } from 'vue'; export default { data() { return { items: [...] } }, methods: { validateChildForms() { const childForms = this.items.map((item, index) => { return inject('form', null, true /* throw error if not found */); }); let isValid = true; for (const childForm of childForms) { if (!childForm.validate()) { isValid = false; } } return isValid; } } } </script> ``` 上述代码中,子组件在 `mounted` 钩子函数中使用 `provide` API将表单对象提供给父组件,父组件使用 `v-for` 指令渲染多个子组件,并且使用 `inject` API获取子组件的表单对象。然后,在父组件中的 `validateChildForms` 方法中,遍历所有子组件的表单对象进行校验。 需要注意的是,这里使用 `inject` API 的第三个参数为 `true`,表示如果没有找到提供的表单对象,则会抛出错误。如果子组件没有提供表单对象,或者表单对象提供的名称不是 `form`,则会抛出错误。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fan 

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值