UniApp 打开文件工具,获取文件类型,判断文件类型

注意:以下代码使用 typeScript 开发,如果想在 js 中使用,可参考 npm 已经发布的包:https://www.npmjs.com/package/uni-easy-file

NPM 使用

如果想直接在 npm 项目中使用可以直接执行以下命令

sh 复制代码
npm i uni-easy-file

然后直接使用

js 复制代码
import {EasyFile} from 'uni-easy-file';

EasyFile.mainName("filePath");

项目源码

参考 github 地址:https://github.com/jl15988/uni-easy-file

主要源码

FileTypes 文件

ts 复制代码
/**
 * 文件类型
 */
class FileTypes {
    imageTypes = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'];

    documentTypes = ['pdf', 'doc', 'docx', 'xls', 'xlsx'];

    videoTypes = ['mp4', 'avi', 'mov', 'rmvb', 'flv', '3gp', 'wmv', 'mkv', 'ts', 'webm', 'm4v'];

    audioTypes = ['mp3', 'wav', 'wma', 'ogg', 'aac', 'flac', 'ape', 'm4a'];

    compressTypes = ['zip', 'rar', '7z', 'tar', 'gz', 'bz2'];

    codeTypes = ['html', 'css', 'js', 'json', 'xml', 'yaml', 'yml', 'sql', 'java', 'py', 'php', 'sh', 'bat', 'cmd', 'ps1', 'go', 'ts', 'vue', 'jsx', 'tsx', 'less', 'scss', 'sass', 'styl', 'coffee', 'md', 'markdown', 'txt'];

    excelTypes = ['xls', 'xlsx'];

    wordTypes = ['doc', 'docx'];

    pptTypes = ['ppt', 'pptx'];

    pdfTypes = ['pdf'];

    textTypes = ['txt'];

    markdownTypes = ['md', 'markdown'];
}

export default new FileTypes()

EasyFile 文件

ts 复制代码
import FileTypes from "./FileTypes";

/**
 * 文件工具
 */
class EasyFile {

    types = FileTypes;

    setTypes(types: any) {
        // @ts-ignore
        this.types = types;
    }

    /**
     * 获取文件名(包括扩展名)
     *
     * ```
     * http://www.example.com/a/b/c.jpg => c.jpg
     * ```
     * @param {string} url 文件地址
     * @return {string}
     */
    fileName(url: string): string {
        if (!url) return '';
        return url.split('/').pop()!;
    }

    /**
     * 获取文件扩展名
     *
     * ```
     * http://www.example/com/a/b/c.jpg => jpg
     * ```
     * @param {string} url 文件地址
     * @return {string}
     */
    extName(url: string): string {
        if (!url) return '';
        return this.fileName(url).split('.').pop()!;
    }

    /**
     * 获取文件主名(不包括扩展名)
     *
     * ```
     * http://www.example.com/a/b/c.jpg => c
     * ```
     * @param {string} url 文件地址
     * @return {string}
     */
    mainName(url: string): string {
        if (!url) return '';
        return this.fileName(url).split('.').shift()!;
    }

    /**
     * 获取文件路径
     *
     * ```
     * http://www.example.com/a/b/c.jpg => http://www.example.com/a/b
     * ```
     * @param {string} url 文件地址
     * @return {string}
     */
    pathName(url: string): string {
        if (!url) return '';
        return url.split('/').slice(0, -1).join('/');
    }

    /**
     * 判断是否是指定类型
     * @param {string} url 文件地址
     * @param {string[]} types 类型数组
     * @return {boolean}
     */
    isType(url: string, types: string[]): boolean {
        const extName = this.extName(url).toLowerCase();
        return types.includes(extName);
    }

    /**
     * 判断是否是图片
     * @param {string} url 文件地址
     * @return {boolean}
     */
    isImage(url: string): boolean {
        return this.isType(url, this.types.imageTypes);
    }

    /**
     * 判断是否是文档
     * @param {string} url 文件地址
     * @return {boolean}
     */
    isDocument(url: string): boolean {
        return this.isType(url, this.types.documentTypes);
    }

    /**
     * 判断是否是视频
     * @param {string} url 文件地址
     * @return {boolean}
     */
    isVideo(url: string): boolean {
        return this.isType(url, this.types.videoTypes);
    }

    /**
     * 判断是否是音频
     * @param {string} url 文件地址
     * @return {boolean}
     */
    isAudio(url: string): boolean {
        return this.isType(url, this.types.audioTypes);
    }

    /**
     * 判断是否是压缩文件
     * @param {string} url 文件地址
     * @return {boolean}
     */
    isCompress(url: string): boolean {
        return this.isType(url, this.types.compressTypes);
    }

    /**
     * 判断是否是代码文件
     * @param {string} url 文件地址
     * @return {boolean}
     */
    isCode(url: string): boolean {
        return this.isType(url, this.types.codeTypes);
    }

    /**
     * 判断是否是Excel文件
     * @param {string} url 文件地址
     * @return {boolean}
     */
    isExcel(url: string): boolean {
        return this.isType(url, this.types.excelTypes);
    }

    /**
     * 判断是否是Word文件
     * @param {string} url 文件地址
     * @return {boolean}
     */
    isWord(url: string): boolean {
        return this.isType(url, this.types.wordTypes);
    }

    /**
     * 判断是否是PPT文件
     * @param {string} url 文件地址
     * @return {boolean}
     */
    isPpt(url: string): boolean {
        return this.isType(url, this.types.pptTypes);
    }

    /**
     * 判断是否是PDF文件
     * @param {string} url 文件地址
     * @return {boolean}
     */
    isPdf(url: string): boolean {
        return this.isType(url, this.types.pdfTypes);
    }

    /**
     * 判断是否是文本文件
     * @param {string} url 文件地址
     * @return {boolean}
     */
    isText(url: string): boolean {
        return this.isType(url, this.types.textTypes);
    }

    /**
     * 判断是否是Markdown文件
     * @param {string} url 文件地址
     * @return {boolean}
     */
    isMarkdown(url: string): boolean {
        return this.isType(url, this.types.markdownTypes);
    }

    /**
     * 判断是否是Office文件
     * @param {string} url 文件地址
     * @return {boolean}
     */
    isOffice(url: string): boolean {
        return this.isWord(url) || this.isExcel(url) || this.isPpt(url);
    }

    /**
     * 判断是否是Office或PDF文件
     * @param {string} url 文件地址
     * @return {boolean}
     */
    isOfficeOrPdf(url: string): boolean {
        return this.isOffice(url) || this.isPdf(url);
    }

    /**
     * 获取文件临时地址
     * @param {string} url 文件地址
     * @return {Promise<string>}
     */
    getFileTempPath(url: string): Promise<string> {
        return new Promise((resolve, reject) => {
            if (!url) {
                reject('文件地址为空');
                return;
            }
            uni.downloadFile({
                url,
                success: (res) => {
                    resolve(res.tempFilePath);
                },
                fail: (e) => {
                    reject(e);
                },
            });
        });
    }

    /**
     * 打开文件
     *
     * 根据文件类型调用不同的api打开文件
     * - 图片类文件调用预览图片(uni.previewImage)
     * - office及pdf类型文件调用打开文档(uni.openDocument)
     * - 其他类型不支持
     * @param {string} url 文件地址
     * @return {Promise<unknown>}
     */
    async openFile(url: string): Promise<unknown> {
        return new Promise(async (resolve, reject) => {
            if (!url) {
                reject('文件地址为空');
                return;
            }
            let tempPath = '';
            try {
                tempPath = await this.getFileTempPath(url);
            } catch (e) {
                reject(e);
                return;
            }
            this.openFileByTempPath(tempPath).then(res => {
                resolve(res);
            }).catch(e => {
                reject(e);
            })
        });
    }

    /**
     * 根据临时地址打开文件
     *
     * 根据文件类型调用不同的api打开文件
     * - 图片类文件调用预览图片(uni.previewImage)
     * - office及pdf类型文件调用打开文档(uni.openDocument)
     * - 其他类型不支持
     * @param {string} tempPath 文件临时地址
     * @return {Promise<unknown>}
     */
    async openFileByTempPath(tempPath: string): Promise<unknown> {
        return new Promise(async (resolve, reject) => {
            if (!tempPath) {
                reject('文件地址为空');
                return;
            }
            if (this.isImage(tempPath)) {
                // 调用微信api预览图片
                uni.previewImage({
                    // 开启时右上角会有三点,点击可以保存
                    showMenu: true,
                    urls: [tempPath],
                    current: tempPath,
                    success: (res) => {
                        resolve(res);
                    },
                    fail: (res) => {
                        reject(res);
                    }
                });
            } else if (this.isOfficeOrPdf(tempPath)) {
                uni.openDocument({
                    filePath: tempPath,
                    // 开启时右上角会有三点,点击可以保存
                    showMenu: true,
                    success: (res) => {
                        resolve(res);
                    },
                    fail: (res) => {
                        reject(res);
                    }
                });
            }
        });
    }

    /**
     * 获取文件 MD5
     *
     * 仅获取文件 MD5 时建议使用此方法,如果同时获取文件大小,建议直接使用 `getFileInfo` 方法
     *
     * | App | H5 | 微信小程序 |
     * | --- | --- | --- |
     * | √ | √ | × |
     *
     * @param {string} url 文件地址
     * @return {Promise<string|undefined>}
     */
    md5(url: string): Promise<string | undefined> {
        return new Promise(async (resolve, reject) => {
            let tempPath = '';
            try {
                tempPath = await this.getFileTempPath(url);
            } catch (e) {
                reject(e);
                return;
            }
            uni.getFileInfo({
                filePath: tempPath,
                digestAlgorithm: 'md5',
                success: (res) => {
                    resolve(res.digest);
                },
                fail: (e) => {
                    reject(e);
                },
            });
        });
    }

    /**
     * 获取文件 SHA1
     *
     * 仅获取文件 SHA1 时建议使用此方法,如果同时获取文件大小,建议直接使用 `getFileInfo` 方法
     *
     * | App | H5 | 微信小程序 |
     * | --- | --- | --- |
     * | √ | √ | × |
     *
     * @param {string} url 文件地址
     * @return {Promise<string|undefined>}
     */
    sha1(url: string): Promise<string | undefined> {
        return new Promise(async (resolve, reject) => {
            let tempPath = '';
            try {
                tempPath = await this.getFileTempPath(url);
            } catch (e) {
                reject(e);
                return;
            }
            uni.getFileInfo({
                filePath: tempPath,
                digestAlgorithm: 'sha1',
                success: (res) => {
                    resolve(res.digest);
                },
                fail: (e) => {
                    reject(e);
                },
            });
        });
    }

    /**
     * 获取文件大小,以字节为单位
     *
     * 仅获取文件大小时建议使用此方法,如果同时获取文件摘要,建议直接使用 `getFileInfo` 方法
     *
     * | App | H5 | 微信小程序 |
     * | --- | --- | --- |
     * | √ | √ | × |
     *
     * @param {string} url 文件地址
     * @return {Promise<number>}
     */
    size(url: string): Promise<number> {
        return new Promise(async (resolve, reject) => {
            let tempPath = '';
            try {
                tempPath = await this.getFileTempPath(url);
            } catch (e) {
                reject(e);
                return;
            }
            uni.getFileInfo({
                filePath: tempPath,
                success: (res) => {
                    resolve(res.size);
                },
                fail: (e) => {
                    reject(e);
                },
            });
        });
    }

    /**
     * 获取文件信息
     *
     * | App | H5 | 微信小程序 |
     * | --- | --- | --- |
     * | √ | √ | × |
     *
     * @param {string} url 文件地址
     * @param {'md5'|'sha1'} digestAlgorithm 摘要算法,支持 md5、sha1
     * @return {Promise<UniApp.GetFileInfoSuccess>}
     */
    getFileInfo(url: string, digestAlgorithm: 'md5' | 'sha1' = 'md5'): Promise<UniApp.GetFileInfoSuccess> {
        return new Promise(async (resolve, reject) => {
            let tempPath = '';
            try {
                tempPath = await this.getFileTempPath(url);
            } catch (e) {
                reject(e);
                return;
            }
            uni.getFileInfo({
                filePath: tempPath,
                digestAlgorithm: digestAlgorithm,
                success: (res) => {
                    resolve(res);
                },
                fail: (e) => {
                    reject(e);
                },
            });
        });
    }
}

export default new EasyFile();
相关推荐
Mr_li10 小时前
给 Vue 开发者的 uni-app 快速指南
vue.js·uni-app
anyup13 小时前
🔥2026最推荐的跨平台方案:H5/小程序/App/鸿蒙,一套代码搞定
前端·uni-app·harmonyos
Mintopia2 天前
Vue3 项目如何迁移到 uni-app x:从纯 Web 到多端应用的系统指南
uni-app
Mintopia2 天前
uni-app x 发展前景技术分析:跨端统一的新阶段?
uni-app
不爱说话郭德纲2 天前
告别漫长的HbuilderX云打包排队!uni-app x 安卓本地打包保姆级教程(附白屏、包体积过大排坑指南)
android·前端·uni-app
HashTang4 天前
【AI 编程实战】第 12 篇:从 0 到 1 的回顾 - 项目总结与 AI 协作心得
前端·uni-app·ai编程
JunjunZ4 天前
uniapp 文件预览:从文件流到多格式预览的完整实现
前端·uni-app
郑州光合科技余经理4 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
TT_Close4 天前
“啪啪啪”三下键盘,极速拉起你的 uni-app 项目!
vue.js·uni-app·前端工程化
特立独行的猫a5 天前
uni-app x跨平台开发实战:开发鸿蒙HarmonyOS影视票房榜组件完整实现过程
华为·uni-app·harmonyos·轮播图·uniapp-x