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

uniapp+Vue3 开发小程序功能(下载文件)

代码如下,复制粘贴即可食用:

<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/590153.html

相关文章:

  • 爬楼梯(js实现,LeetCode:70)
  • 每天五分钟深度学习PyTorch:循环神经网络RNN的计算以及维度信息
  • css的显示模式
  • Redis----大key、热key解决方案、脑裂问题
  • Matlab 舰载机自动着舰控制系统研究
  • SpringMVC(四)Restful软件架构风格
  • 【从零开始学习计算机科学】算法分析(三)动态规划 与 贪心算法
  • STM32---FreeRTOS事件标志组
  • 数学建模:MATLAB循环神经网络
  • PostgreSQL教程(二)九大类型
  • 第27周JavaSpringboot git初识
  • 如何在Django中设置CSRF Token?
  • 【计算机网络】浏览器组成、工作原理、页面渲染流程...
  • 什么是 Fisher 信息矩阵
  • JDBC数据库连接池技术详解——从传统连接方式到高效连接管理
  • Android Dagger2 框架注入模块源码深度剖析(四)
  • matlab图论分析之指标计算(二)
  • CSS @media print 使用详解
  • Flutter_学习记录_状态管理之GetX
  • 通过物联网与可视化技术搭建的智慧工地管理云平台,java智慧工地源代码,企业级源码