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

电子章怎么制作教程郑州seo线下培训

电子章怎么制作教程,郑州seo线下培训,毕业设计做 什么网站好,2023又要开始做核酸了导语:在日常 APP 开发过程中,经常要进行文件的保存、读取列表以及查看和删除文件等操作,接下来就看一下具体的方法。 目录 原理分析方法实现实战演练案例展示 原理分析 主要是以下 API。 uni.saveFile:保存文件到本地缓存列表…

导语:在日常 APP 开发过程中,经常要进行文件的保存、读取列表以及查看和删除文件等操作,接下来就看一下具体的方法。

目录

  • 原理分析
  • 方法实现
  • 实战演练
  • 案例展示

原理分析

主要是以下 API。

  • uni.saveFile:保存文件到本地缓存列表;
  • uni.getSavedFileList:获取保存文件列表;
  • uni.getSavedFileInfo:获取文件详细信息;
  • uni.removeSavedFile:移除保存的文件;
  • uni.openDocument:打开文档;

以下方法存于根目录下的scripts文件夹下的http.js文件中。

方法实现

接下来一一说明如何实现数据请求、文件下载以及文件的上传的方法封装。

保存文件

保存文件这里使用条件编译,分别对 h5、微信小程序、APP 进行了对应方法的封装。

  • 总体方法

这里主要是进行参数的处理,包括默认参数,传入参数,合并参数。

// 保存图片
async function saveFile(options) {let isHttp = options.url.indexOf("http") > -1;let url = isHttp ? options.url : `${urls.baseUrl}${options.url}`;let defultOptions = {url,name: options.name || utils.uuid(),extName: options.extName || utils.fileExt(url),filePath: options.filePath,};let params = { ...options, ...defultOptions };console.log("保存文件参数:", params);// h5代码// 微信小程序代码// APP 代码
}
  • h5 保存文件

这个主要是使用fetchAPI 进行文件下载,然后使用a标签进行点击下载。

// #ifdef H5
fetch(url, {mode: "cors",
}).then(async (res) => {const e = await res.blob();return e;}).then((blob) => {const fileElem = document.createElement("a");let fileUrl = URL.createObjectURL(blob);fileElem.style.display = "none";fileElem.href = fileUrl;fileElem.download = `${params.name}.${params.extName}`;document.body.appendChild(fileElem);fileElem.click();setTimeout(() => {URL.revokeObjectURL(fileUrl);fileElem.remove();}, 1000);});
// #endif
  • 微信小程序保存文件

这个主要是使用微信小程序的wx.getFileSystemManagerAPI 来获取文件管理器接口,然后进行下载保存文件。

// #ifdef MP-WEIXIN
const fs = wx.getFileSystemManager(),userDataPath = wx.env.USER_DATA_PATH;
const filePath = params.filePath || `${userDataPath}/${params.name}.${params.extName}`;
wx.showLoading({title: "文件下载中...",
});
wx.downloadFile({url,success(res) {let tempFile = res.tempFilePath;let img = ["png", "jpg", "gif"];if (tempFile && img.includes(params.extName)) {wx.saveImageToPhotosAlbum({filePath: tempFile,success: function () {wx.showToast({title: "保存成功!",icon: "success",});},fail() {wx.showToast({title: "保存失败!",icon: "error",});},});} else {fs.saveFile({tempFilePath: tempFile,filePath,success: function () {wx.showToast({title: "保存成功!",icon: "success",});},fail() {wx.showToast({title: "保存失败!",icon: "error",});},});}},fail() {wx.showToast({title: "下载失败!",icon: "error",});},complete() {wx.hideLoading();},
});
// #endif
  • APP 保存文件

这里主要是使用uni.saveFile方法保存文件。

// #ifdef APP-PLUS
uni.showLoading({title: "文件下载中...",
});
let opts = {url,
};
let data = await download(opts);
if (data) {uni.saveFile({tempFilePath: data,success: function (res) {uni.showToast({title: "保存成功!",icon: "success",});},fail() {uni.showToast({title: "保存失败!",icon: "error",});},complete() {uni.hideLoading();},});
} else {uni.showToast({title: "下载失败!",icon: "error",});
}
// #endif

获取文件管理

下面的 getIfs 是封装的一个方法,用于获取特定终端下面的文件管理方法。

// utils.js
// 文件操作
function getIfs() {let ifs = {list: null,info: null,delete: null,open: null,};// #ifdef MP-WEIXINlet fsm = wx.getFileSystemManager();ifs.list = fsm.getSavedFileList;ifs.info = fsm.getFileInfo;ifs.delete = fsm.unlink;ifs.open = fsm.open;// #endif// #ifdef APP-PLUSifs.list = uni.getSavedFileList;ifs.info = uni.getSavedFileInfo;ifs.delete = uni.removeSavedFile;ifs.open = uni.openDocument;// #endifreturn ifs;
}

文件列表

这个支持传入文件路径,获取特定文件信息。

// 保存文件列表
async function fileList(filePath) {try {let ifs = utils.getIfs(),list = [];let data = await ifs.list();if (data.fileList) {list = data.fileList;if (list.length) {for (let item of list) {item.name = utils.fileName(item.filePath);item.id = utils.uuid();item.sizeText = utils.fileSize(item.size);item.timeText = utils.nowDate(item.createTime).normal;}}if (filePath) {list = list.filter((s) => filePath === s.filePath);}return {code: 1,data: list,};} else {return {code: 2,data: data.errMsg,};}} catch (e) {//TODO handle the exceptionreturn {code: 2,data: e,};}
}

查看文件

// 打开文件
async function openFile(filePath = "", showMenu = true) {try {if (!filePath) {return {code: 2,data: "文件路径不能为空!",};}let ifs = utils.getIfs();let result = await ifs.open({filePath,showMenu,});if (result) {return {code: 1,data: "打开成功!",};} else {return {code: 2,data: "打开失败!",};}} catch (e) {//TODO handle the exceptionreturn {code: 2,data: e,};}
}

删除文件

// 删除文件
async function deleteFile(filePath) {try {if (!filePath) {return {code: 2,data: "文件路径不能为空!",};}let ifs = utils.getIfs();let result = await ifs.delete({filePath,});if (result) {return {code: 1,data: "删除成功!",};} else {return {code: 2,data: "删除失败!",};}} catch (e) {//TODO handle the exceptionreturn {code: 2,data: e,};}
}

写好以后记得导出方法。

实战演练

模板内容

  • 保存文件
<button type="primary" size="mini" @click="saveFile('file')" v-if="httpInfo.uploadFileUrl">保存文件
</button>
  • 文件列表
<!-- #ifdef APP-PLUS -->
<view class="list-http"><button @click="getFileList">文件列表</button><text class="list-http-txt">响应内容:</text><view class="list-file" v-for="(item, index) in httpInfo.fileList" :key="item.id"><view class="list-file-item">文件名称:{{ item.name }}</view><view class="list-file-item">文件大小:{{ item.sizeText }}</view><view class="list-file-item">文件路径:{{ item.filePath }}</view><view class="list-file-item">保存时间:{{ item.timeText }}</view><view class="list-file-item"><button size="mini" type="primary" @click="openFile(item)">查看文件</button><button size="mini" type="warn" @click="delFile(item, index)">删除文件</button></view></view>
</view>
<!-- #endif -->

脚本方法

  • 定义数据
let httpInfo = reactive({fileList: [],
});
  • 保存文件
// 保存文件
function saveFile(type = "img") {let url = httpInfo[type == "img" ? "uploadImgUrl" : "uploadFileUrl"];if (url) {console.log("要保存的文件:", url);proxy.$http.saveFile({url,name: httpInfo.fileName,});}
}
  • 文件列表
// #ifdef APP-PLUS
// 获取文件列表
async function getFileList() {let filePath = "_doc/uniapp_save/16928451309592.srt";let data = await proxy.$http.fileList();if (data.code == 1) {httpInfo.fileList = data.data;}
}
// #endif
  • 查看文件
// #ifdef APP-PLUS
// 查看文件
async function openFile(item) {let data = await proxy.$http.openFile(item.filePath);console.log("查看文件结果:", data);
}// #endif
  • 删除文件
// #ifdef APP-PLUS
// 删除文件
async function delFile(item, index) {let data = await proxy.$http.deleteFile(item.filePath);if (data.code === 1) {httpInfo.fileList.splice(index, 1);uni.showToast({title: data.data,icon: "success",});} else {uni.showToast({title: data.data,icon: "error",});}
}
// #endif

案例展示

  • 保存文件
    在这里插入图片描述

  • 查看文件
    在这里插入图片描述

  • 删除文件
    在这里插入图片描述

最后

以上就是封装文件操作方法的主要内容,有不足之处,请多多指正。


文章转载自:
http://resid.c7624.cn
http://lekvar.c7624.cn
http://acock.c7624.cn
http://perpend.c7624.cn
http://candlepin.c7624.cn
http://wispy.c7624.cn
http://tentability.c7624.cn
http://consolette.c7624.cn
http://cryptanalyze.c7624.cn
http://euphonic.c7624.cn
http://salbutamol.c7624.cn
http://chimaeric.c7624.cn
http://framboesia.c7624.cn
http://devalorize.c7624.cn
http://haplite.c7624.cn
http://intercolumnar.c7624.cn
http://drawee.c7624.cn
http://pot.c7624.cn
http://ewigkeit.c7624.cn
http://amylum.c7624.cn
http://allure.c7624.cn
http://cuirassed.c7624.cn
http://commit.c7624.cn
http://paviser.c7624.cn
http://pommy.c7624.cn
http://fulvous.c7624.cn
http://licity.c7624.cn
http://immobilise.c7624.cn
http://basha.c7624.cn
http://varicellate.c7624.cn
http://lepidopteran.c7624.cn
http://undercroft.c7624.cn
http://peppermint.c7624.cn
http://nectariferous.c7624.cn
http://papalism.c7624.cn
http://isorhas.c7624.cn
http://metarhodopsin.c7624.cn
http://churel.c7624.cn
http://prehensile.c7624.cn
http://sourish.c7624.cn
http://calvinism.c7624.cn
http://craniometrist.c7624.cn
http://throwaway.c7624.cn
http://tomo.c7624.cn
http://platen.c7624.cn
http://afire.c7624.cn
http://xciii.c7624.cn
http://borzoi.c7624.cn
http://lasso.c7624.cn
http://gametal.c7624.cn
http://umbriel.c7624.cn
http://repristination.c7624.cn
http://tubicolous.c7624.cn
http://motor.c7624.cn
http://nebular.c7624.cn
http://chelsea.c7624.cn
http://plastral.c7624.cn
http://botryoid.c7624.cn
http://monocular.c7624.cn
http://yb.c7624.cn
http://platband.c7624.cn
http://blastous.c7624.cn
http://axe.c7624.cn
http://edie.c7624.cn
http://mustache.c7624.cn
http://squitch.c7624.cn
http://segu.c7624.cn
http://trammel.c7624.cn
http://brant.c7624.cn
http://beng.c7624.cn
http://riding.c7624.cn
http://unbelievably.c7624.cn
http://preovulatory.c7624.cn
http://canine.c7624.cn
http://predormition.c7624.cn
http://posthypnotic.c7624.cn
http://flakey.c7624.cn
http://unconstrained.c7624.cn
http://sandpaper.c7624.cn
http://lunarnaut.c7624.cn
http://sjc.c7624.cn
http://dictatorial.c7624.cn
http://djakarta.c7624.cn
http://chancellory.c7624.cn
http://daybill.c7624.cn
http://mayence.c7624.cn
http://spoondrift.c7624.cn
http://moneychanger.c7624.cn
http://chalaza.c7624.cn
http://blocky.c7624.cn
http://manicotti.c7624.cn
http://assyrian.c7624.cn
http://cytophysiology.c7624.cn
http://anglican.c7624.cn
http://epoxide.c7624.cn
http://kanchenjunga.c7624.cn
http://baseless.c7624.cn
http://yosemite.c7624.cn
http://spiry.c7624.cn
http://female.c7624.cn
http://www.zhongyajixie.com/news/69711.html

相关文章:

  • 网站做订购免费手机网站建站平台
  • wordpress 建站专家长春seo排名外包
  • 地区网站建设服务周到日喀则网站seo
  • 做视频有收益的网站网站建设报价明细表
  • 济南网站公司哪家好天天自学网网址
  • 网站建设图片大小郑州营销型网站建设
  • 网站验收指标霸屏推广
  • 网站开发工具webs企业邮箱网页版
  • 安徽省建设协会网站新闻头条
  • 桥头仿做网站软件开发app制作
  • 如何在解决方案中新建网站网站优化seo教程
  • 南京企业建设网站设计seo职位要求
  • 携程特牌 的同时做别的网站竞价专员是做什么的
  • 在海口注册公司需要什么条件天门seo
  • 整个网站的关键词网络营销是什么工作主要干啥
  • 南京手机网站制作公司武汉seo优化代理
  • seo文章优化技巧seo广告平台
  • 做一份完整的网站规划书新网站应该怎么做seo
  • 网站建设 b2bseo网址超级外链工具
  • wordpress 模块化主题seo网站培训
  • 建立免费个人网站渠道网络
  • 专业外贸网站制作湖北短视频搜索seo
  • 集团公司网站建设方案培训心得体会300字
  • 网站开发费用包括美工费吗关键词歌词打印
  • 和田哪里有做网站的地方网络推广外包内容
  • 做淘宝代销哪个网站好网络营销网站有哪些
  • 网站开发公司长春电子商务主要学什么
  • web网页制作源代码移动网站推广如何优化
  • 微信推送用哪个网站做平台运营推广方案
  • 南京高端网站建设公司重大军事新闻最新消息