当前位置: 首页 > news >正文

uniapp v3+ts 使用 u-upload上传图片以及视频

上传图片方法 

<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="file" multiple :maxCount="6"></u-upload>
// maxCount 最大上传数const fileList1 = ref([]);const file = ref([])// 删除图片const deletePic = (event : any) => {fileList1.value.splice(event.index, 1);file.value.splice(event.index, 1);// console.log(fileList1.value); file};// 新增图片const afterRead = async (event : any) => {// 当设置 mutiple 为 true 时, file 为数组格式,否则为对象格式let lists = [].concat(event.file);let fileListLen = fileList1.value.length;lists.map((item) => {fileList1.value.push({...item,status: 'uploading',message: '上传中',});});for (let i = 0; i < lists.length; i++) {const result = await uploadFilePromise(lists[i].url);let item = fileList1.value[fileListLen];fileList1.value.splice(fileListLen, 1, {...item,status: 'success',message: '',url: result,});fileListLen++;}};const uploadFilePromise = (url : any) => {return new Promise((resolve, reject) => {let a = uni.uploadFile({url: import.meta.env.VITE_APP_BASE_URL + 'file/image', // 图片上传地址filePath: url,name: 'file',header: {"site-id": import.meta.env.VITE_SITE_ID,"token": getToken()},formData: {},success: (res) => {let respic = JSON.parse(res.data)console.log(respic.data.url, '图片');let obj = respic.data.urlfile.value.push(obj)setTimeout(() => {resolve(res.data.data);}, 1000);},});});};

上传视频 方法

 <u-upload accept="video" :fileList="videoFileList" @afterRead="videoAfterRead"@delete="deleteVideo " name="3" multiple :maxCount="1"></u-upload>const videoFileList = ref([])const videoAfterRead = async (event : any) => {let lists = [].concat(event.file);let fileListLen = videoFileList.value.length;lists.map((item) => {videoFileList.value.push({...item,status: 'uploading',message: '上传中',});});for (let i = 0; i < lists.length; i++) {const result = await uploadVideoFile(lists[i].url);let item = videoFileList.value[fileListLen];videoFileList.value.splice(fileListLen, 1, {...item,status: 'success',message: '',url: result,});fileListLen++;}};const videoValue = ref('')const uploadVideoFile = (url : any) => {return new Promise((resolve, reject) => {let a = uni.uploadFile({// 后端上传视频地址url: import.meta.env.VITE_APP_BASE_URL + 'file/video', // 视频上传地址filePath: url,name: 'file',// header 携带内容 根据自己后端要求header: {"site-id": import.meta.env.VITE_SITE_ID,"token": getToken()},success: (res) => {res.data = JSON.parse(res.data)// console.log(res.data, '测试');videoValue.value = res.data.data.url// console.log(videoValue.value, '测试');// 这个状态值要根据后端接口返的 若对不上 则视频不会回显,一直显示上传中if (res.data.code == 1) {setTimeout(() => {resolve(res.data.url)}, 1000)}},fail: (error) => {console.log(error, '上次失败的原因');uni.showToast({title: '上传失败',icon: 'none'});reject(error)}});});};const deleteVideo = (event : any) => {videoFileList.value.splice(event.index, 1);videoValue.value = '';}

点击提交时 要传递视频 跟图片的地址值

const submit = () => {let params = {images: file.value,video: videoValue.value}console.log(params, '发布的参数');// 执行自己的请求 todo}

http://www.lryc.cn/news/208922.html

相关文章:

  • 为什么虚拟dom会提高性能?
  • 2015年亚太杯APMCM数学建模大赛A题海上丝绸之路发展战略的影响求解全过程文档及程序
  • js中HTMLCollection如何循环
  • Kafka - 3.x 副本不完全指北
  • 二分归并法将两个数组合并
  • ROS自学笔记十六:URDF优化_xacro文件
  • XMLHttpRequest拦截请求和响应
  • 前端 读取/导入 Excel文档
  • 聊聊springboot的TomcatMetricsBinder
  • 《动手学深度学习 Pytorch版》 10.6 自注意力和位置编码
  • 2023年第四届MathorCup高校数学建模挑战赛——大数据竞赛B题 实现代码
  • larvel 中的api.php_Laravel 开发 API
  • 虚拟机构建部署单体项目及前后端分离项目
  • C++之特殊类的设计
  • Java练习题2020 -1
  • LuaTable转C#的列表List和字典Dictionary
  • Redis快速上手篇七(集群)
  • Mac 安装nvm
  • python 从mssql取出datetime2类型之后格式化
  • 18.2 使用NPCAP库抓取数据包
  • pytest-yaml 测试平台-3.创建执行任务定时执行用例
  • 安卓文件资源中,一个字串包含引用其他字串的写法
  • 解决:谷歌浏览器访问http时,自动转https访问的问题
  • MQTT协议和边缘计算
  • Redis(04)| 数据结构-压缩列表
  • 516 最长回文子序列(区间DP)(灵神笔记)
  • Kafka - 异步/同步发送API
  • 嵌套for循环在外层循环和内层循环中使用两个Executors.newCachedThreadPool缓存线程池执行操作
  • 【uniapp+云函数调用】人脸识别,实人认证,适用于app,具体思路解析,已实现
  • 系列十六、bean有哪些生命周期的回调方法?有哪几种实现方式?