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

小程序中下载文件 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>


http://www.kler.cn/a/576637.html

相关文章:

  • 嵌入式开发之串行数据处理
  • OpenCV GrabCut 图像分割
  • iSpiik产品说:抖音短视频,为什么开了自动连播?
  • 日语学习-日语知识点小记-构建基础-JLPT-N4N5阶段(17):会话练习
  • 【Linux实践系列】:用c语言实现一个shell外壳程序
  • android中activity1和activity2中接收定时消息
  • 【selenium工具操作web页面中的下拉框元素 】
  • 机器学习的下一个前沿是因果推理吗?——探索机器学习的未来方向!
  • 工具介绍《HACKBAR V2》
  • leetcode面试题-------链表相交
  • c语言笔记 fgets
  • Maven匹配机制和仓库库设置
  • *算法中的数据结构(3)
  • 【神经网络】python实现神经网络(一)——数据集获取
  • 【hello git】git rebase、git merge、git stash、git cherry-pick
  • 实现Django和Transformers 构建智能客服大模型(模拟订单系统)
  • 【每日学点HarmonyOS Next知识】双向传值问题、子组件半径、VIdeo标签下载隐藏、字符串替换、路由问题
  • 2025年科技趋势深度解析:从“人工智能+”到量子跃迁的技术革命
  • Qt:多线程
  • 通过Nacos API实现微服务不间断部署