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

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>


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

相关文章:

  • 【C++】 —— 笔试刷题day_1
  • thinkphp5对接腾讯云文字识别ocr试卷切题
  • kali linux web扫描工具
  • electron获取鼠标在浏览器外的点击和拖拽等动作
  • Python中很常用的100个函数整理
  • Linux网络编程——网络编程初识、UDP套接字简单了解
  • 三次握手与四次挥手
  • vscode arm拓展 keil acm5 到acm6迁移
  • 【透视国家的三维棱镜:技术、制度与文化的解构与重构】
  • 知识库Dify和cherry无法解析影印pdf word解决方案
  • 数据安全基石:备份文件的重要性与自动化实践
  • 【iOS逆向与安全】sms短信转发插件与上传服务器开发
  • Rust 模式匹配中的可反驳性与不可反驳性
  • Docker数据管理,端口映射与容器互联
  • 【网络安全工程】任务11:路由器配置与静态路由配置
  • 用Python实现PDF转Doc格式小程序
  • 一篇文章巩固技术-----设计模式
  • 安固软件上网行为管理软件:提升企业效率与安全的双重保障
  • 【leetcode hot 100 2】两数相加
  • volatile 在 JVM 层面的实现机制