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

uniapp+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>

相关文章:

  • QT系列教程(14) QT 按键事件
  • 【sentry】sentry报错信息转发常用办公聊天工具
  • 嵌入式工控机在汽车制造中的卓越表现
  • 本地Docker部署雷池WAF让网站安全监测更简单无需复杂配置
  • 根据开始和结束日期,获取每一天和每个月的开始和结束日期的list
  • IDEA接入阿里云百炼中免费的通义千问[2025版]
  • 简单记录一下Oracle数据库与mysql数据库注入的不同。
  • 配置安全网站
  • c++_队列习题
  • 【每日八股】计算机网络篇(四):HTTP
  • stm32 f4 flash 调用时卡死
  • Flask Jinja语法总结篇
  • MySQL8.0窗口函数
  • 请谈谈 TypeScript 中的接口(interface)和类型别名(type alias),它们的区别是什么?
  • ThinkPHP8.0+MySQL8.0搭建简单实用电子证书查询系统
  • Kafka×DeepSeek:智能决策破取经八十一难!
  • 若依-导出后端解析
  • MySQL性能调优实战手册:从慢查询到执行计划全解析
  • springmvc想要一个模块使用相同的url的前缀,怎么处理
  • 配置 Thunderbird 以使用 outlook 邮箱
  • 泰安诚信的企业建站公司/全网投放广告的渠道有哪些
  • 政府门户网站建设的现状/爱站网关键词挖掘工具站长工具
  • 重庆代还信用卡网站建设/北京网站优化服务
  • 芜湖建设机械网站/网络服务投诉平台
  • 专业企业网站设计/上海seo推广方法
  • 网站备案 核验单/免费cms建站系统