uniapp多格式文件选择(APP,H5)

uniapp多格式文件选择(APP,H5)

背景

从手机选择文件进行上传是移动端很常见的需求,在原生开发时由于平台专一性很容易实现。但是用uniapp开发官方提供的API在APP平台只能选择图片和视频,选择其他格式的File也只有H5平台提供了相关API。虽然通过NativeJs也能实现选择多格式文件,但是需要对原生开发有一定的了解,而且不太稳定。所以本文提供一个能抹平平台差异,并且通用的方式来选择手机端的多格式文件,但是只限于APP和H5平台。先上个Android端的效果图:

实现

思路是通过renderjs来实现,通过在renderjs中创建一个input为file类型的dom元素,然后手动触发dom的click方法,则会弹出文件选择框(如上)。

代码实现

从上截图可以知道,页面上就一个按钮,通过点击按钮触发renderjs中的函数,然后执行dom相关操作,代码相对简单,这里就直接上代码:

javascript 复制代码
<template>
	<view class="content">
		<button @click="fileChoose">文件选择</button>
		<view :fileData="fileData" :change:fileData="renderJS.receiveFileData"/>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				fileData: ''
			}
		},
		onLoad() {},
		methods: {
			fileChoose(){
				this.fileData = 'test'
				//这里将fileData定时置空,否则下次点击不会触发renderjs函数
				setTimeout(()=> {
					this.fileData = ''
				},1000)
			},
			//从renderjs中触发的函数回调
			receiveRenderFile(result){
				console.log(result)
			}
		}
	}
</script>
<script module="renderJS" lang="renderjs">
	export default {
		data() {
			return {}
		},
		mounted() {
			console.log('1111111111mounted')
		},
		methods: {
			receiveFileData(newValue, oldValue, ownerVm, vm){
				if(!newValue){
					return
				}
				this.createFileInputDom(ownerVm)
			},
			//手动创建dom
			createFileInputDom(ownerVm){
				let fileInput = document.createElement('input')
				//设置input为file类型
				fileInput.setAttribute('type','file')
				//设置file格式为素有
				fileInput.setAttribute('accept','*')
				//手动触发dom点击事件
				fileInput.click()
				fileInput.addEventListener('change', e => {
					//获取file对象
					let file = e.target.files[0]
					//转化为URL路径
					const filePath = URL.createObjectURL(file)
					//将文件名和文件路径回调到视图层
					ownerVm.callMethod('receiveRenderFile',{
						name: file.name,
						filePath: filePath
					})
					//事件回调之后销毁dom
					fileInput = null
				})
			}
		}
	}
</script>
<style>
</style>

运行结果

我们这里选择手机一个为工作助手的apk文件

receiveRenderFile函数中打印如下:

javascript 复制代码
{
    "name": "工作助手.apk",
    "filePath": "blob:file:///07f057dc-b51b-445c-9fb8-8ec9c64075c9"
}

拿到名字和路径之后我们就可以使用uni.uploadFile相关API进行上传。

注意事项

由于本人使用的是Android手机,苹果手机上未尝试过这种方式,iOS平台不能保证有用(理论上来说应该是支持的)。

尾巴

今天的文章就到这里了,希望能给大家帮助,如果喜欢我的文章,欢迎给我点赞,评论,关注,谢谢大家!

相关推荐
三声三视14 小时前
uni-app 鸿蒙端传参变成 [object Object]?顺着源码追到 ArkTS router 底层才搞明白
人工智能·ai·uni-app·aigc·ai编程·harmonyos
小徐_23331 天前
Open Wot 1.0.5 发布:让 AI 接入 wot-ui,只需要两条命令
前端·uni-app·ai编程
AI多Agent协作实战派1 天前
AI多Agent协作系统实战(二十二):从6列到12列——任务监控报告的进化之路
java·人工智能·uni-app·bug
小杨小杨、努力变强!1 天前
VS Code运行HBuilder X中的uni-app项目
vscode·uni-app·uni-app run
码兄科技2 天前
实战:基于Spring Boot + UniApp的地理信息小程序开发
spring boot·后端·uni-app
2501_916007472 天前
iOS和macOS应用程序性能分析和优化工具使用综合指南
android·macos·ios·小程序·uni-app·iphone·webview
2501_916007473 天前
深入理解HTTPS对称与非对称加密机制及Charles抓包实践
网络协议·http·ios·小程序·https·uni-app·iphone
小徐_23333 天前
AI 写 wot-ui 总在猜 API?我们把 Skills、MCP 和 CLI 都配好了
前端·uni-app·ai编程
Liu.7743 天前
uni-app 组件 uni-easyinput 常见 Bug 及解决方案
uni-app·bug
小徐_23334 天前
uni-app 项目别再从零搭了!3 个 Wot UI 起手模板怎么选?
前端·uni-app