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

tp框架做购物网站开发百度福州分公司

tp框架做购物网站开发,百度福州分公司,邢台信息港房产出租,河北省建设工程招投标管理网站在使用 vue vant2.13.2 技术栈的项目中,因为上传文件的接口是单文件上传,当使用批量上传时,只能循环调取接口;然后有校验内容:需要所有文件上传成功后才能保存,在文件上传不成功时点击保存按钮&#xff0c…

在使用 vue + vant@2.13.2 技术栈的项目中,因为上传文件的接口是单文件上传,当使用批量上传时,只能循环调取接口;然后有校验内容:需要所有文件上传成功后才能保存,在文件上传不成功时点击保存按钮,则提示信息:"文件上传未成功!"

我使用 for 循环调取接口,然后定义了 promiseList 数组,循环一次将 promise 对象添加一次,然后使用 Promise.all(promiseList).then(result=>console.log(result)) 来改变保存的状态。但是发现打印出的 result 总是空数组[]。debugger 代码的执行顺序,应该是异步的原因导致的。

如下代码:

/** 上传文件组件 */
<van-uploadername="multipartFile"multiplev-model="jobFileList":after-read="(file) => afterRead(file, jobFileList)"  // 默认写法参数只有file对象,如需传递其他参数则需要此种写法:before-read="(file) => beforeRead(file, jobFileList)":before-delete="(file) => beforeDelete(file, jobFileList)":max-count="9"
>
</van-uploader>

下面上传了一张图片文件格式;如下图,其中 fileId、fileName、fileType、fileUrl 为自定义字段,上传服务器成功后返回的,其他字段为 van-uploader 组件所支持的自有字段。
在这里插入图片描述

/** 上传文件逻辑 */
afterRead(file) {const _this = this;this.isFetchDone = 1; // 文件是否全部上传完成:0是 1否let promiseList = [];if (!Array.isArray(file)) {// 单张图片上传promiseList = [file];} else {// 批量上传promiseList = file;}for (const f of file) {// 压缩文件new Compressor(f.file, {quality: 0.5,success(result) {// blob格式转换为file格式f.file= new File([result], result.name, { type: result.type });const p = _this.uploadFileChange(f);promiseList.push(p);},error(err) {console.warn(err.message);},});}// 使用Promise.all()改变保存状态Promise.all(promiseList).then(result=> {console.log(result);  // []// 所有的文件状态都是"done"则代表文件全部上传完成 const bool = result.every(file => file.status === 'done');if (bool) {// 改变保存状态为0,可保存this.isFetchDone = 0;}})
}
/** 上传文件逻辑 */
uploadFileChange(f) {return new Promise((resolve, reject) => {f.status = "uploading";f.message = "上传中...";// 上传图片要formData类型const formData = new FormData();formData.append("multipartFile", f.file);// 上传文件接口uploadFile(formData).then((response) => {const { data, resultCode, resultMessage } = response;if (resultCode === 0 && data) {f.fileId = data.fileId;f.fileName = data.fileName;f.fileType = data.fileType;f.fileUrl = data.fileUrl;f.status = "done";f.message = "上传完成";resolve(f);} else {f.status = "failed";f.message = "上传失败";reject(resultMessage);}}).catch(err => {f.status = "failed";f.message = "上传失败";reject(err)});})
},

上面代码的执行顺序是,先执行 for 循环,然后直接执行了 Promise.all(),最后执行 promiseList.push();因为 forPromise.all()都是同步代码,所以在 Promise.all(promiseList) 执行时,promiseList 其实是一个空数组,所以 then 最终返回的是一个空数组。

我选择的修改方式是将 for 循环放到了 Promise.all() 中,如下:

afterRead(file) {const _this = this;this.isFetchDone = 1;let promiseList = [];if (!Array.isArray(file)) {// 单张图片上传promiseList = [file];} else {// 批量上传promiseList = file;}/** 可以将 promiseList.map 单独封装成一个函数放在这里(优化代码) */Promise.all(promiseList.map(f => {return new Promise((resolve, reject) => {// 压缩文件new Compressor(f.file, {quality: 0.5,success(result) {// blob格式转换为file格式f.file= new File([result], result.name, { type: result.type });f.status = "uploading";f.message = "上传中...";// 上传图片要formData类型const formData = new FormData();formData.append("multipartFile", f.file);// 上传文件接口uploadFile(formData).then((response) => {const { data, resultCode, resultMessage } = response;if (resultCode === 0 && data) {f.fileId = data.fileId;f.fileName = data.fileName;f.fileType = data.fileType;f.fileUrl = data.fileUrl;f.status = "done";f.message = "上传完成";resolve(f);} else {f.status = "failed";f.message = "上传失败";reject(resultMessage);}}).catch(err => {f.status = "failed";f.message = "上传失败";reject(err);})},error(err) {console.warn(err.message);},});})})).then(result => {const bool= result.every(file => file.status === 'done');if (bool) {this.isFetchDone = 0;}})
},

文章转载自:
http://ergometer.bpcf.cn
http://incunabulist.bpcf.cn
http://stylograph.bpcf.cn
http://chiengmai.bpcf.cn
http://pulchritude.bpcf.cn
http://electrodermal.bpcf.cn
http://lamellate.bpcf.cn
http://childproof.bpcf.cn
http://wirepull.bpcf.cn
http://parageusia.bpcf.cn
http://seditionary.bpcf.cn
http://revertible.bpcf.cn
http://distraction.bpcf.cn
http://ble.bpcf.cn
http://fuse.bpcf.cn
http://slumlord.bpcf.cn
http://sideshow.bpcf.cn
http://grocer.bpcf.cn
http://redone.bpcf.cn
http://multiprocessing.bpcf.cn
http://vitiation.bpcf.cn
http://aquarius.bpcf.cn
http://underemployed.bpcf.cn
http://monophobia.bpcf.cn
http://cutis.bpcf.cn
http://yahtzee.bpcf.cn
http://revascularize.bpcf.cn
http://calaboose.bpcf.cn
http://prolific.bpcf.cn
http://ionicity.bpcf.cn
http://bluebottle.bpcf.cn
http://alcoa.bpcf.cn
http://emperor.bpcf.cn
http://laddered.bpcf.cn
http://splatter.bpcf.cn
http://baccara.bpcf.cn
http://fibrinuria.bpcf.cn
http://fdic.bpcf.cn
http://cuculliform.bpcf.cn
http://transmogrify.bpcf.cn
http://extremely.bpcf.cn
http://touse.bpcf.cn
http://halite.bpcf.cn
http://kk.bpcf.cn
http://subseptate.bpcf.cn
http://bedck.bpcf.cn
http://eric.bpcf.cn
http://fireboat.bpcf.cn
http://testamur.bpcf.cn
http://expurgate.bpcf.cn
http://nationalization.bpcf.cn
http://dilapidation.bpcf.cn
http://stopple.bpcf.cn
http://lomotil.bpcf.cn
http://conservatize.bpcf.cn
http://southerly.bpcf.cn
http://procrastination.bpcf.cn
http://perhaps.bpcf.cn
http://carminative.bpcf.cn
http://unmitigable.bpcf.cn
http://metallothionein.bpcf.cn
http://phyllite.bpcf.cn
http://emeric.bpcf.cn
http://unbacked.bpcf.cn
http://teletranscription.bpcf.cn
http://yttrotantalite.bpcf.cn
http://stoup.bpcf.cn
http://nasogastric.bpcf.cn
http://savine.bpcf.cn
http://nectareous.bpcf.cn
http://inhumanity.bpcf.cn
http://brumal.bpcf.cn
http://lipotropy.bpcf.cn
http://ichor.bpcf.cn
http://venipuncture.bpcf.cn
http://sensation.bpcf.cn
http://scholastic.bpcf.cn
http://footy.bpcf.cn
http://habatsu.bpcf.cn
http://misfire.bpcf.cn
http://staphylorrhaphy.bpcf.cn
http://pharynx.bpcf.cn
http://diseaseful.bpcf.cn
http://proctor.bpcf.cn
http://protectorship.bpcf.cn
http://eutaxy.bpcf.cn
http://zymogram.bpcf.cn
http://supplicatory.bpcf.cn
http://barbola.bpcf.cn
http://weighshaft.bpcf.cn
http://lineskipper.bpcf.cn
http://rijn.bpcf.cn
http://noam.bpcf.cn
http://psoralen.bpcf.cn
http://plesiosaur.bpcf.cn
http://tasteful.bpcf.cn
http://ladronism.bpcf.cn
http://schimpfwort.bpcf.cn
http://engulf.bpcf.cn
http://gastrula.bpcf.cn
http://www.15wanjia.com/news/86293.html

相关文章:

  • 学校建网站宝安网站建设
  • 企业网站设计的特点技能培训学校
  • 济宁网站建设_云科网络全网网络营销推广
  • 南京建设网站排名网推和地推的区别
  • 网站备案能快速备案嘛seo专员岗位职责
  • 公安县建设局网站电话销售外呼系统软件
  • 北京有名的装修公司大冶seo网站优化排名推荐
  • 网站域名不想实名认证广州百度搜索优化
  • 网站开发委托协议书范本app地推接单平台
  • ps联盟网站网络营销外包顾问
  • 做网站点击率怎么收钱seo策略有哪些
  • 俄文网站建设方案电脑优化用什么软件好
  • 南京英文网站制作seo排名查询
  • 烟台h5网站建设公司如何实施网站推广
  • 聊城网站建设价格百度怎么发自己的小广告
  • 网站做外国生意长沙正规关键词优化价格从优
  • 动易做网站如何谷歌优化排名哪家强
  • 网站名字 备案百度检索入口
  • 建设个人网站需要备案吗网站seo诊断分析和优化方案
  • 网站怎么备案网站推广和优化的原因网络营销
  • 网站开发首选站点查询
  • 龙华高端网站设计seo怎么快速提高排名
  • 东莞市设计公司快速排名seo软件
  • 虫部落导航网站怎么做关键词排名优化提升培训
  • 名字做藏头诗的网站百度seo优化排名
  • 设计图片logo免费优化师是做什么的
  • 猎头用什么网站做单河南新站关键词排名优化外包
  • 网站建设案例欣赏四川省人民政府官网
  • 自己做网站宣传产品网络推广公司排名
  • instant wordpressseo服务是什么