vue3上传图片

说明:action="http://192.168.1.188:8080/upload/admin"//上传图片的路径(一般有提供上传路径,写在标签上的)

1.上传多张图片,最多5张

<!-- html中 -->
<el-form-item label="商品图片" label-width="100px" style="width:100%;">
  <el-upload v-model="form.imgList" :limit="5" class="avatar-uploader"
    action="http://192.168.1.188:8080/upload/admin" list-type="picture-card" :headers="headers"
    :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload" :on-exceed="handleLimit"
    :on-remove="handleRemove">
    <el-icon class="avatar-uploader-icon">
      <Plus />
    </el-icon>
  </el-upload>
</el-form-item>
<script setup lang="ts">
import { Plus, Upload } from '@element-plus/icons-vue'//引入图标
import { ElMessage, ElMessageBox } from 'element-plus'

// 上传图片
// 上传图片的请求头
const headers = reactive({
  "AdminToken": Cookies.get("token")
})

//上传成功后的回调
const handleAvatarSuccess: UploadProps['onSuccess'] = (
  response,
  uploadFile
) => {
  form.value.imgList.push(response.url)
  form.value.img = form.value.imgList[0]
}

// 图片移除的回调函数
const handleRemove: UploadProps['onRemove'] = (uploadFile, uploadFiles) => {
  form.value.imgList = uploadFiles
}
// 图片上传前的函数
const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg' && rawFile.type !== 'image/png' && rawFile.type !== 'image/webp') {
    ElMessage.error('Avatar picture must be JPG format!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('图片大小不能超过2MB!')
    return false
  }
  return true
}
// 超过图片限制数量是的回调函数
function handleLimit() {
  ElMessage.error('图片不能超过5张!')
}
</script>
//css
<style lang="less" scoped>
.avatar-uploader .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }

    .avatar-uploaders .avatars {
      width: 78px;
      height: 78px;
      display: block;
    }
</style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>

2.上传1张图片

<!-- html中 -->
<el-form-item label="广告图片" label-width="200px" style="width:100%;" v-show="picRequire" id="uploadImg"
    :style="{ background: form.color }">
    <el-upload class="avatar-uploader" action="http://192.168.1.188:8080/upload/admin" :show-file-list="false"
      :headers="headers" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
      <img v-if="form.imgUrl" :src="form.imgUrl" class="avatar" />
      <el-icon v-else class="avatar-uploader-icon">
        <Plus />
      </el-icon>
    </el-upload>
  </el-form-item>
<script setup lang="ts">
import { Plus, Search } from '@element-plus/icons-vue'//引入图标
import { ElMessage, ElMessageBox, type FormInstance, type UploadProps } from 'element-plus'

// 上传图片
// action="http://192.168.1.188:8080/upload/admin"//上传图片的路径(一般有提供上传路径,写在标签上的)

// 上传图片的请求头
const headers = reactive({
  "AdminToken": Cookies.get("token")
})
// 成功后的回调
const handleAvatarSuccess: UploadProps['onSuccess'] = (
  response,
  uploadFile
) => {
  form.value.imgUrl = response.url
  //将背景色统一为第一个像素点的封装函数(按照需求是否引入调用)
  canvasImgColor(form.value.imgUrl, (e: string) => {
    form.value.color = e
  })
}
// 上传图片前的回调
const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg' && rawFile.type !== 'image/png' && rawFile.type !== 'image/webp') {
    ElMessage.error('Avatar picture must be JPG format!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 1) {
    ElMessage.error('Avatar picture size can not exceed 1MB!')
    return false
  }
  return true
}
<script>
//css中
<style lang="less" scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}

3.上传图片是element ui的一个组件具体看官网

TinyMCE Vue 是一个将 TinyMCE(一个流行的富文本编辑器)集成到 Vue.js 应用中的组件。要在 Vue3 中使用 TinyMCE 配合上传图片,首先你需要安装相关的依赖和配置TinyMCE。以下是一个简单的示例: 1. 安装依赖: ```bash npm install tinymce @tinymce/tinymce-vue vue-file-upload --save ``` 2. 在 main.js 或者 vue.config.js 中引入并配置Vue文件上传插件(这里使用vue-file-upload为例): ```javascript import VueFileUpload from 'vue-file-upload'; import '@tinymce/tinymce-vue/dist/tinymce.vue'; Vue.use(VueFileUpload); ``` 3. 在组件中引入TinyMCE,并配置图片上传功能: ```html <template> <div> <tinymce-vue :config="tinymceConfig" v-model="editorContent" @file-added="handleImageUpload" ></tinymce-vue> </div> </template> <script> export default { data() { return { editorContent: '', tinymceConfig: { plugins: 'image', toolbar: 'image', images_upload_url: '/api/upload-image', // 图片上传的API URL images_upload_base64: true, // 是否支持base64上传 images_rendition_target_width: 800, // 图片缩略图宽度 }, }; }, methods: { handleImageUpload(file) { this.$fileUpload.upload(file).then((response) => { // 处理上传成功的响应,比如更新富文本中的src属性 const imageUrl = response.url; const imgElement = document.createElement('img'); imgElement.src = imageUrl; this.editorContent = this.editorContent.replace(/\[\[image\]\]/, imgElement.outerHTML); }); }, }, }; </script> ``` 在这个示例中,`tinymceConfig` 配置了TinyMCE的插件、工具栏以及图片上传的URL。当用户在编辑器中添加图片时,`handleImageUpload`方法会被调用,然后通过VueFileUpload上传图片,并更新富文本内容中的图片链接。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值