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

小程序中下载文件 Vue3 写法

小程序下载文件,可以先预览文件内容,然后在手机上打开文件的工具中选择保存。

简单示例:(复制到HBuilder直接食用即可)

<template>
	<view class="container-detail">
		<view class="example-body" @click="openFile(item.url)" v-for="(item, index) in fileList" :key="index">
			<image class="images" src="/common/images/files.png" mode="aspectFit"></image>
			<view class="name">
				{{item.name}}
			</view>
		</view>
		
	</view>
</template>

<script setup>
	let fileList = ref([
		{
			"name":"file1.jpg",
			"extname":"jpg",
			"url":"https://images.dog.ceo/breeds/retriever-curly/n02099429_817.jpg",
		},
		{
			"name":"file2.jpg",
			"extname":"jpg",
			"url":"https://images.dog.ceo/breeds/doberman/n02107142_16400.jpg",
		},
		{
			"name":"somefile.pdf",
			"extname":"pdf",
			"url":"https://example.com/somefile.pdf",
		}
	])
	function openFile(url) {
	      const isImgType = ['jpg', 'png', 'bmp', 'jpeg', 'webp']
	      uni.showLoading({ title: '加载中...' })
	      uni.downloadFile({
	        url,
	        success: (res) => {
	          const fileType = res.tempFilePath.split('.').pop()
	          if (isImgType.includes(fileType)) {
	            uni.previewImage({ // 调用微信api预览图片
	              showmenu: true, // 开启时右上角会有三点,点击可以保存
	              urls: [res.tempFilePath],
	              current: res.tempFilePath,
	              success: (res) => {
	                uni.hideLoading()
	                console.log('打开图片成功')
	              },
	              fail: (res) => {
					  uni.hideLoading()
	                console.log(res)
	                console.log('打开图片失败')
	              },
	            })
	          } else {
	            uni.openDocument({
	              filePath: res.tempFilePath,
	              showMenu: true, // 开启时右上角会有三点,点击可以保存
	              success: (res) => {
	                uni.hideLoading()
	                console.log('打开文档成功')
	              },
	              fail: (res) => {
					  uni.hideLoading()
	                console.log(res)
	                console.log('打开文档失败')
	              },
	            })
	          }
	        },
	        fail: (e) => {
				uni.hideLoading()
	          console.log(e)
	        },
	      })
	    }

</script>

<style lang="scss" scoped>
.container-detail {
	padding: 30rpx;
	.example-body {
		padding: 10rpx 0;
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 32rpx;
		.images {
			width: 40rpx;
			height: 40rpx;
			image {
				width: 100%;
				height: 100%;
			}
		}
		.name {
			flex: 1;
			font-size: 28rpx;
			font-family: PingFang HK, PingFang HK-400;
			font-weight: 400;
			text-align: LEFT;
			color: royalblue;
			margin-left: 22rpx;
		}
	}
}
</style>

相关文章:

  • Linux(Centos 7.6)命令详解:vim
  • bert模型笔记
  • vim基本操作及常用命令
  • 【PLL】分频器:其他拓扑
  • Linux 进程管理
  • 大白话html语义化标签优势与应用场景
  • git如何解除远程仓库 改变远程仓库地址
  • Elasticsearch为索引设置自动时间戳,ES自动时间戳
  • 杂项知识笔记搜集
  • 【由技及道】量子跃迁部署术:docker+jenkins+Harbor+SSH的十一维交付矩阵【人工智障AI2077的开发日志011】
  • Java算法语法学习 美丽子集的数目 - 力扣 Map接口
  • 2025软件测试面试八股文(含答案+文档)
  • 传输协议优化的博弈三角
  • Nginx 跨域配置详细讲解
  • DeepSeek 助力 Vue3 开发:打造丝滑的表格(Table)之功能优化,基础加载状态,Table8基础加载状态
  • 【并发编程】聊聊定时任务ScheduledThreadPool的实现原理和源码解析
  • java字符串
  • 【Linux】线程同步与互斥
  • 如何在PHP爬虫中处理异常情况的详细指南
  • unsigned类型与signed类型的区别介绍
  • 专门做免费东西试吃的网站/网页设计制作教程
  • 建设政府网站多少钱/关键词排名优化公司哪家好
  • 政府网站建设 江苏省/网络营销成功案例有哪些2022
  • 深圳装饰企业前50强/北京云无限优化
  • 淘宝客wordpress主题/seo外包公司专家
  • 欧美做愛网站/百度竞价排名查询