首页 > 其他分享 >图片上传-1.弹窗切片VueCropper

图片上传-1.弹窗切片VueCropper

时间:2024-11-14 11:22:44浏览次数:1  
标签:const 上传 value VueCropper cropper data options 弹窗

<template>
  <div class="user-info-head" @click="editCropper()">
    <img :src="options.img" title="点击上传头像" class="img-circle img-lg" />
    <el-dialog v-model="open" :title="title" width="800px" append-to-body @opened="modalOpened" @close="closeDialog">
      <el-row>
        <el-col :xs="24" :md="12" :style="{ height: '350px' }">
          <vue-cropper
            v-if="visible"
            ref="cropper"
            :img="options.img"
            :info="true"
            :auto-crop="options.autoCrop"
            :auto-crop-width="options.autoCropWidth"
            :auto-crop-height="options.autoCropHeight"
            :fixed-box="options.fixedBox"
            :output-type="options.outputType"
            @real-time="realTime"
          />
        </el-col>
        <el-col :xs="24" :md="12" :style="{ height: '350px' }">
          <div class="avatar-upload-preview">
            <img :src="options.previews.url" :style="options.previews.img" />
          </div>
        </el-col>
      </el-row>
      <br />
      <el-row>
        <el-col :lg="2" :md="2">
          <el-upload action="#" :http-request="requestUpload" :show-file-list="false" :before-upload="beforeUpload">
            <el-button>
              选择
              <el-icon class="el-icon--right">
                <Upload />
              </el-icon>
            </el-button>
          </el-upload>
        </el-col>
        <el-col :lg="{ span: 1, offset: 2 }" :md="2">
          <el-button icon="Plus" @click="changeScale(1)"></el-button>
        </el-col>
        <el-col :lg="{ span: 1, offset: 1 }" :md="2">
          <el-button icon="Minus" @click="changeScale(-1)"></el-button>
        </el-col>
        <el-col :lg="{ span: 1, offset: 1 }" :md="2">
          <el-button icon="RefreshLeft" @click="rotateLeft()"></el-button>
        </el-col>
        <el-col :lg="{ span: 1, offset: 1 }" :md="2">
          <el-button icon="RefreshRight" @click="rotateRight()"></el-button>
        </el-col>
        <el-col :lg="{ span: 2, offset: 6 }" :md="2">
          <el-button type="primary" @click="uploadImg()">提 交</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import 'vue-cropper/dist/index.css';
import { VueCropper } from 'vue-cropper';
import { uploadAvatar } from '@/api/system/user';  上传接口  
/**
 * 用户头像上传
 * @param data 头像文件
 */
export const uploadAvatar = (data: FormData) => {
  return request({
    url: '/xxx/xxx/xxx/xxx',
    method: 'post',
    data: data
  });
};
import useUserStore from '@/store/modules/user';
import { UploadRawFile } from 'element-plus';

interface Options {
  img: string | any; // 裁剪图片的地址
  autoCrop: boolean; // 是否默认生成截图框
  autoCropWidth: number; // 默认生成截图框宽度
  autoCropHeight: number; // 默认生成截图框高度
  fixedBox: boolean; // 固定截图框大小 不允许改变
  fileName: string;
  previews: any; // 预览数据
  outputType: string;
  visible: boolean;
}

const userStore = useUserStore();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const open = ref(false);
const visible = ref(false);
const title = ref('修改头像');

const cropper = ref<any>({});
//图片裁剪数据
const options = reactive<Options>({
  img: userStore.avatar,
  autoCrop: true,
  autoCropWidth: 200,
  autoCropHeight: 200,
  fixedBox: true,
  outputType: 'png',
  fileName: '',
  previews: {},
  visible: false
});

/** 编辑头像 */
const editCropper = () => {
  open.value = true;
};
/** 打开弹出层结束时的回调 */
const modalOpened = () => {
  visible.value = true;
};
/** 覆盖默认上传行为 */
const requestUpload = (): any => {};
/** 向左旋转 */
const rotateLeft = () => {
  cropper.value.rotateLeft();
};
/** 向右旋转 */
const rotateRight = () => {
  cropper.value.rotateRight();
};
/** 图片缩放 */
const changeScale = (num: number) => {
  num = num || 1;
  cropper.value.changeScale(num);
};
/** 上传预处理 */
const beforeUpload = (file: UploadRawFile): any => {
  if (file.type.indexOf('image/') == -1) {
    proxy?.$modal.msgError('文件格式错误,请上传图片类型,如:JPG,PNG后缀的文件。');
  } else {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => {
      options.img = reader.result;
      options.fileName = file.name;
    };
  }
};
/** 上传图片 */
const uploadImg = async () => {
  cropper.value.getCropBlob(async (data: any) => {
    let formData = new FormData();
    formData.append('avatarfile', data, options.fileName);
    const res = await uploadAvatar(formData);
    open.value = false;
    options.img = res.data.imgUrl;
    userStore.setAvatar(options.img);
    proxy?.$modal.msgSuccess('修改成功');
    visible.value = false;
  });
};
/** 实时预览 */
const realTime = (data: any) => {
  options.previews = data;
};
/** 关闭窗口 */
const closeDialog = () => {
  options.img = userStore.avatar;
  options.visible = false;
};
</script>

<style lang="scss" scoped>
.user-info-head {
  position: relative;
  display: inline-block;
  height: 120px;
}

.user-info-head:hover:after {
  content: '+';
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  color: #eee;
  background: rgba(0, 0, 0, 0.5);
  font-size: 24px;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  cursor: pointer;
  line-height: 110px;
  border-radius: 50%;
}
</style>

标签:const,上传,value,VueCropper,cropper,data,options,弹窗
From: https://www.cnblogs.com/baozhengrui/p/18545611

相关文章

  • 文件上传
    <el-dropdown-itemicon="Top"@click="handleImport">导入数据</el-dropdown-item><!--用户导入对话框--><el-dialogv-model="upload.open":title="upload.title"width="400px"append-to-body>......
  • Java 项目注入、上传、搜索及插件挖掘的代码审计
    目录Java项目注入、上传、搜索及插件挖掘的代码审计一、什么是Java项目注入、上传、搜索及插件挖掘的代码审计二、原理(一)注入原理(二)文件上传原理(三)搜索功能安全原理(四)插件安全原理三、步骤与代码示例(一)准备工作(二)注入攻击审计步骤与代码示例(三)文件上传审计步骤与......
  • 如何在 Spring MVC 中处理 AJAX 请求:从表单数据到文件上传的全流程
    个人名片......
  • 2024-11-13 uniapp自定义全局弹窗并可以通过uni来调用【转载】
    新建三个文件: dialog.js:exportdefault{/*链接处理*/getLink(params){leturl="/components/dialog/index";if(params){letparamStr="";for(letnameinparams){param......
  • 11.12 ali-oss上传图片
    11.12ali-oss上传图片1.上传到服务器:@PostMapping("/upload")publicStringupload(MultipartFilefile){if(file.isEmpty()){return"图片上传失败";}System.out.println();StringOriginalfileName=......
  • web安全漏洞之文件上传
    文件上传       1什么是文件上传   文件上传就比如上传用户头像,上传图片,上传附件等。在服务端实现文件上传功能时,如果对用户上传的文件没有做好处理,就有可能导致十分严重的安全问题,比如被上传木马文件造成RCE(远程代码执行) php文件上传的基本代码 ......
  • WEB 漏洞 - 文件上传之解析漏洞与编辑器安全
    目录WEB漏洞-文件上传之解析漏洞与编辑器安全一、漏洞概述二、原理分析三、实现步骤与代码示例一、漏洞概述在WEB应用中,文件上传功能是一个常见的模块,但它也存在着诸多安全隐患。文件上传的解析漏洞以及编辑器相关的安全问题就是其中重要的部分。解析漏洞是指......
  • Qml 中的那些坑(七)---ComboBox嵌入Popup时,滚动内容超过其可见区域不会关闭ComboBox弹
    【写在前面】最近在写信息提交(表单)的窗口时发现一个奇怪的BUG:其代码如下:importQtQuick2.15importQtQuick.Controls2.15importQtQuick.Window2.15Window{width:640height:480visible:truetitle:qsTr("HelloWorld")Button{......
  • openresty配置为简单文件上传下载服务器
    安装resty.upload模块opminstallledgetech/lua-resty-upload新建/usr/local/openresty/nginx/lua/upload.lua内容如下:localupload=require"resty.upload"localcjson=require"cjson.safe"localchunk_size=4096localform,err=upload:new(c......
  • JavaWeb:文件上传2
    欢迎来到“雪碧聊技术”CSDN博客!在这里,您将踏入一个专注于Java开发技术的知识殿堂。无论您是Java编程的初学者,还是具有一定经验的开发者,相信我的博客都能为您提供宝贵的学习资源和实用技巧。作为您的技术向导,我将不断探索Java的深邃世界,分享最新的技术动态、实战经验以及项目......