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

响应式网站建站建站平台在线提交功能

响应式网站建站,建站平台在线提交功能,淘宝客网站程序购米,从事网站建设的职业注意:以下代码使用 typeScript 开发,如果想在 js 中使用,可参考 npm 已经发布的包:https://www.npmjs.com/package/uni-easy-file NPM 使用 如果想直接在 npm 项目中使用可以直接执行以下命令 npm i uni-easy-file然后直接使用 …

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

NPM 使用

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

npm i uni-easy-file

然后直接使用

import {EasyFile} from 'uni-easy-file';EasyFile.mainName("filePath");

项目源码

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

主要源码

FileTypes 文件

/*** 文件类型*/
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 文件

import FileTypes from "./FileTypes";/*** 文件工具*/
class EasyFile {types = FileTypes;setTypes(types: any) {// @ts-ignorethis.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();
http://www.rdtb.cn/news/15856.html

相关文章:

  • 国家信用信息公示系统湖北seo优化啥意思
  • 湖州建设培训入口网站网站优化及推广
  • 做个人网站需要多少钱百度网盘资源分享
  • 网站栅格化怎么做武汉seo网络优化公司
  • 电商企业有哪些广告优化
  • 不买服务器做网站最新国际军事动态
  • 在线购物网站建设东莞网络推广及优化
  • 加快网站速度南昌seo排名公司
  • 如何用dw做asp动态网站百度热搜榜排名今日头条
  • 做化工的网站优化网站价格
  • 现在的网站怎样做推广智能建站
  • a站为什么不火了今天刚刚发生的新闻最新新闻
  • 厦门做网站优化百度seo关键词优化排行
  • 长沙制作网页网站站长工具免费
  • 临沂河东区建设局网站厦门搜索引擎优化
  • 经典的jq查询网站网络营销可以做什么工作
  • 青岛建设银行股份有限公司网站首页杭州搜索引擎推广排名技术
  • 阿里云服务器 做网站免费seo关键词优化服务
  • html5 css3单页手机网站模板网络营销课程个人感悟
  • 咸宁网站建设解决方案推广普通话的意义
  • 珠海网站快速排名提升百度商家
  • CSS3网站建设自助建站系统哪个好用
  • 抖音直播开放平台seo在线培训
  • 郑州网站建设方案服务营业推广策划方案
  • 加盟的网站建设厦门seo服务
  • 做视频网站的公司有哪些百度账号注册中心
  • 打赏网站开发安徽seo
  • 网站建设技术服务清单网站推广策略
  • 论坛排名网站关键词如何优化上首页
  • redbubble网站开发东莞企业推广网站制作