uniapp 打开文件管理器上传(H5、微信小程序、android app三端)文件

2023-12-08 07:20

本文主要是介绍uniapp 打开文件管理器上传(H5、微信小程序、android app三端)文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

H5跟安卓APP 手机打开的效果图:

Vue页面:

<template><view class="content"><button @click="uploadFiles">点击上传</button></view>
</template><script>export default {data() {return {}},methods: {//h5、微信小程序、app上传文件uploadFiles() {//#ifdef H5uni.chooseFile({count: 1,extension: ['.doc,.xlsx,.docx'],success: res => {uni.showLoading({title: '导入中...',mask: true});uni.uploadFile({url: this.$BASE_URL + 'api/uploads/upload',file: res.tempFiles[0],name: 'file',success: (res) => {},});}});//#endif// #ifdef MP-WEIXINuni.chooseMessageFile({count: 1, //默认100success: res => {uni.showLoading({title: '导入中...',mask: true});uni.uploadFile({url: this.$BASE_URL + 'api/uploads/upload',filePath: res.tempFiles[0].path,name: 'file',success: (res) => {},});}});//#endif// #ifdef APP-VUE//在这里导入打开安卓app本地文件选择器的封装方法this.$common.androidChooseFile(res => {var tempFiles = res;uni.uploadFile({url: this.$BASE_URL + 'api/uploads/upload',filePath: tempFiles,name: 'file',success: (res) => {}});},'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')// #endif},},}
</script><style scoped></style>

封装的公共方法库utils/common.js: 

var common = {}
//安卓本地文件选择器
common.androidChooseFile = (callback, acceptType) => {var CODE_REQUEST = 1000;var main = plus.android.runtimeMainActivity();if (plus.os.name == 'Android') {var Intent = plus.android.importClass('android.content.Intent');var intent = new Intent(Intent.ACTION_GET_CONTENT);intent.addCategory(Intent.CATEGORY_OPENABLE);if (acceptType) {intent.setType(acceptType);} else {intent.setType("*/*");}main.onActivityResult = (requestCode, resultCode, data) => {if (requestCode == CODE_REQUEST) {const uri = data.getData();plus.android.importClass(uri);const Build = plus.android.importClass('android.os.Build');const isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;const DocumentsContract = plus.android.importClass('android.provider.DocumentsContract');if (isKitKat && DocumentsContract.isDocumentUri(main, uri)) {if ("com.android.externalstorage.documents" == uri.getAuthority()) {console.log("6666");var docId = DocumentsContract.getDocumentId(uri);var split = docId.split(":");var type = split[0];if ("primary" == type) {var Environment = plus.android.importClass('android.os.Environment');callback(Environment.getExternalStorageDirectory() + "/" + split[1]);} else {var System = plus.android.importClass('java.lang.System');var sdPath = System.getenv("SECONDARY_STORAGE");if (sdPath) {callback(sdPath + "/" + split[1]);}}} else if ("com.android.providers.downloads.documents" == uri.getAuthority()) {var id = DocumentsContract.getDocumentId(uri);var ContentUris = plus.android.importClass('android.content.ContentUris');var contentUri = ContentUris.withAppendedId(//    Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));Uri.parse("content://downloads/public_downloads"), id);callback(getDataColumn(main, contentUri, null, null));} else if ("com.android.providers.media.documents" == uri.getAuthority()) {var docId = DocumentsContract.getDocumentId(uri);var split = docId.split(":");console.log(split);var type = split[0];console.log(type);var MediaStore = plus.android.importClass('android.provider.MediaStore');if ("image" == type) {contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;} else if ("video" == type) {contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;} else if ("audio" == type) {contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;} else {contentUri = MediaStore.Files.getContentUri("external");}console.log(contentUri);var selection = "_id=?";var selectionArgs = new Array();selectionArgs[0] = split[1];callback(getDataColumn(main, contentUri, selection, selectionArgs));}} else if ("content" == uri.getScheme()) {callback(getDataColumn(main, uri, null, null));} else if ("file" == uri.getScheme()) {callback(uri.getPath());}}}main.startActivityForResult(intent, CODE_REQUEST);}function getDataColumn(main, uri, selection, selectionArgs) {plus.android.importClass(main.getContentResolver());let cursor = main.getContentResolver().query(uri, ['_data'], selection, selectionArgs,null);plus.android.importClass(cursor);if (cursor != null && cursor.moveToFirst()) {var column_index = cursor.getColumnIndexOrThrow('_data');var result = cursor.getString(column_index)cursor.close();return result;}return null;}
}
export default common

这篇关于uniapp 打开文件管理器上传(H5、微信小程序、android app三端)文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/469013

相关文章

Python基于微信OCR引擎实现高效图片文字识别

《Python基于微信OCR引擎实现高效图片文字识别》这篇文章主要为大家详细介绍了一款基于微信OCR引擎的图片文字识别桌面应用开发全过程,可以实现从图片拖拽识别到文字提取,感兴趣的小伙伴可以跟随小编一... 目录一、项目概述1.1 开发背景1.2 技术选型1.3 核心优势二、功能详解2.1 核心功能模块2.

python编写朋克风格的天气查询程序

《python编写朋克风格的天气查询程序》这篇文章主要为大家详细介绍了一个基于Python的桌面应用程序,使用了tkinter库来创建图形用户界面并通过requests库调用Open-MeteoAPI... 目录工具介绍工具使用说明python脚本内容如何运行脚本工具介绍这个天气查询工具是一个基于 Pyt

Ubuntu设置程序开机自启动的操作步骤

《Ubuntu设置程序开机自启动的操作步骤》在部署程序到边缘端时,我们总希望可以通电即启动我们写好的程序,本篇博客用以记录如何在ubuntu开机执行某条命令或者某个可执行程序,需要的朋友可以参考下... 目录1、概述2、图形界面设置3、设置为Systemd服务1、概述测试环境:Ubuntu22.04 带图

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

Python程序打包exe,单文件和多文件方式

《Python程序打包exe,单文件和多文件方式》:本文主要介绍Python程序打包exe,单文件和多文件方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python 脚本打成exe文件安装Pyinstaller准备一个ico图标打包方式一(适用于文件较少的程

VS配置好Qt环境之后但无法打开ui界面的问题解决

《VS配置好Qt环境之后但无法打开ui界面的问题解决》本文主要介绍了VS配置好Qt环境之后但无法打开ui界面的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目UKeLvb录找到Qt安装目录中designer.UKeLvBexe的路径找到vs中的解决方案资源

Python程序的文件头部声明小结

《Python程序的文件头部声明小结》在Python文件的顶部声明编码通常是必须的,尤其是在处理非ASCII字符时,下面就来介绍一下两种头部文件声明,具有一定的参考价值,感兴趣的可以了解一下... 目录一、# coding=utf-8二、#!/usr/bin/env python三、运行Python程序四、

如何基于Python开发一个微信自动化工具

《如何基于Python开发一个微信自动化工具》在当今数字化办公场景中,自动化工具已成为提升工作效率的利器,本文将深入剖析一个基于Python的微信自动化工具开发全过程,有需要的小伙伴可以了解下... 目录概述功能全景1. 核心功能模块2. 特色功能效果展示1. 主界面概览2. 定时任务配置3. 操作日志演示

GitLab文件的上传与下载方式

《GitLab文件的上传与下载方式》:本文主要介绍GitLab文件的上传与下载方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录GitLab 项目拉取到本地GitLab 项目上传方法方法 1:本地项目未初始化Git方法 2:本地项目已初始化GitGitLab 上

Redis迷你版微信抢红包实战

《Redis迷你版微信抢红包实战》本文主要介绍了Redis迷你版微信抢红包实战... 目录1 思路分析1.1hCckRX 流程1.2 注意点①拆红包:二倍均值算法②发红包:list③抢红包&记录:hset2 代码实现2.1 拆红包splitRedPacket2.2 发红包sendRedPacket2.3 抢