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

dedecms网站栏目管理深圳seo公司助力网络营销飞跃

dedecms网站栏目管理,深圳seo公司助力网络营销飞跃,交互设计师和ui设计师的区别,品牌推广的目的背景 文件管理页面,后端只提供了一个根据 file_path 和 file_name 参数下载文件的API接口。产品需要支持用户多选之后的批量下载功能。 技术实现 基础代码 先调用下载接口,获取到二进制的文件流,然后通过 a 标签完成下载。 // return [r…

背景

文件管理页面,后端只提供了一个根据 file_pathfile_name 参数下载文件的API接口。产品需要支持用户多选之后的批量下载功能。

技术实现

基础代码

先调用下载接口,获取到二进制的文件流,然后通过 a 标签完成下载。

// @return [response, error] 如果请求失败,则 error 有值,response 为null,否则 error 为null
const requestNormalFn = (url, method, data, {params = {}, requestConfig = {}, unitModuleName = null, deleteWithBody = false
} = {}) => {const requestUuid = genUuid()const reqBody = (method === 'delete' && !deleteWithBody) ? null : datareturn wrapAwait(myFetch.request({method, url, data: reqBody, params, requestConfig, requestUuid, unitModuleName}))
}// 通过 click a 标签下载文件
let downloadcount = 1
const saveDataToFileForHdf = (data, filename, ext) => {downloadcount ++let url = URL.createObjectURL(data);let link = document.createElement('a');link.setAttribute('href', url);link.setAttribute('download', `${filename}.${ext}`);link.addEventListener('click', function (e) {console.log('click', downloadcount, e.target)})document.body.appendChild(link);link.click();document.body.removeChild(link);
};

方案一

循环选中的行,在循环中调用下载文件的接口

const downloadFile = async row => {const [response, error] = await requestNormalFn(downloadUrl, 'get', null, {params: {file_path: row.file_path,file_name: row.file_name},requestConfig: {responseType: 'blob'}})if (error) {ElMessage({type: 'error',message: `${row.file_name}下载失败`})return}const fileNameArr = row.file_name.split('.')const ext = fileNameArr.splice(fileNameArr.length - 1, 1)const fileName = fileNameArr.join('.')saveDataToFileForHdf(response, fileName, ext[0])
}const onBatchDownloadClick = rows => {rows.forEach(row => downloadFile(row))
}

问题
当勾选的数据 >= 6时,就开始出现实际下载下来的文件数量小于勾选的数据量,能下载下来的文件数不稳定。

思考
有以下可能:

  1. 请求结果丢失 —— 后续验证被排除,没有丢失。
  2. 生成 a 标签的时候,click 这里被某种安全策略阻止 —— 后续证明,click 里面都能被打印出来。

所以那么最后只能指向下载文件的问题,查阅之后知道浏览器有安全策略,如果下载操作过于频繁或者数量过多,仍可能被视为不安全的操作而被阻止。

方案二

const requestFile = async row => {const [res, err] = await requestNormalFn(downloadUrl, 'get', null, {params: {file_path: row.file_path,file_name: row.file_name},requestConfig: {responseType: 'blob'}})return [res, err, row]
}const batchDownload = async (rows) => {const requestList = rows.map(row => requestFile(row))const resList = await Promise.allSettled(requestList)resList.forEach(res => {const [response, error, row] = res.valueif (error) {failedList.value.push(row.file_name)return}const fileNameArr = row.file_name.split('.')const ext = fileNameArr.splice(fileNameArr.length - 1, 1)const fileName = fileNameArr.join('.')saveDataToFileForHdf(response, fileName, ext[0])})if (failedList.value.length) {ElMessage({message: `${failedList.value.join(', ')} 下载失败。`,type: 'error'})}return [0, null]
}// 将长数组分片,进行处理
const dealPartForArray = async (list, cb, batchLength = 10) => {const splitNum = Math.ceil(list.length / batchLength)for (let i = 0; i < splitNum; i++) {const splitList = list.slice(i * batchLength, (i + 1) * batchLength)const [, err] = await cb(splitList)}
}
// 点击批量下载按钮的时候,将 选中的行 分片处理,每片最多6个数据
const onBatchDownloadClick = rows => {dealPartForArray(rows, batchDownload, 6)
}

这里最好wait一点时间之后,再执行下一个分片的下载。可以解决问题。但是提示信息需要优化,考虑一次的用户操作,更友好的提示用户。

方案三

交由后端处理,让后端提供批量下载的接口,这里要考虑到当批量下载回来的数据很大时,前端和后端需要做的事情。(下载压缩文件,以及返回数据的分片)

总结

最好是用方案三,如果后端不支持的话,再考虑方案二。


文章转载自:
http://urgency.rymd.cn
http://thundersheet.rymd.cn
http://bomb.rymd.cn
http://rosella.rymd.cn
http://bridging.rymd.cn
http://clotheshorse.rymd.cn
http://biostatics.rymd.cn
http://militancy.rymd.cn
http://viscousness.rymd.cn
http://veritable.rymd.cn
http://equilibrant.rymd.cn
http://hejaz.rymd.cn
http://hesperornis.rymd.cn
http://unpresented.rymd.cn
http://junction.rymd.cn
http://unbelievable.rymd.cn
http://bulrush.rymd.cn
http://tandem.rymd.cn
http://bashaw.rymd.cn
http://stooge.rymd.cn
http://teacherless.rymd.cn
http://runty.rymd.cn
http://rsj.rymd.cn
http://pearlash.rymd.cn
http://astringency.rymd.cn
http://mesorectum.rymd.cn
http://leaguer.rymd.cn
http://himalayas.rymd.cn
http://masty.rymd.cn
http://knot.rymd.cn
http://apophthegm.rymd.cn
http://telangiectasia.rymd.cn
http://bunchy.rymd.cn
http://mainsail.rymd.cn
http://during.rymd.cn
http://emulous.rymd.cn
http://toposcopy.rymd.cn
http://obtruncate.rymd.cn
http://recirculate.rymd.cn
http://mutilate.rymd.cn
http://classical.rymd.cn
http://crimson.rymd.cn
http://chloroform.rymd.cn
http://ciliation.rymd.cn
http://rheumatology.rymd.cn
http://escheator.rymd.cn
http://mullion.rymd.cn
http://numen.rymd.cn
http://dlemocrat.rymd.cn
http://hydrogenolysis.rymd.cn
http://abusage.rymd.cn
http://phenacaine.rymd.cn
http://exogenic.rymd.cn
http://mistranslate.rymd.cn
http://metagalaxy.rymd.cn
http://radiopaque.rymd.cn
http://vociferator.rymd.cn
http://canaanite.rymd.cn
http://enviably.rymd.cn
http://dismountable.rymd.cn
http://fostress.rymd.cn
http://cong.rymd.cn
http://huxley.rymd.cn
http://favourable.rymd.cn
http://president.rymd.cn
http://toupee.rymd.cn
http://immunogenesis.rymd.cn
http://swazzle.rymd.cn
http://galactosidase.rymd.cn
http://mucific.rymd.cn
http://toiletry.rymd.cn
http://herr.rymd.cn
http://proclamatory.rymd.cn
http://mockery.rymd.cn
http://hypercholia.rymd.cn
http://beshrew.rymd.cn
http://bothnia.rymd.cn
http://tsimmes.rymd.cn
http://macrencephalia.rymd.cn
http://twentieth.rymd.cn
http://fledging.rymd.cn
http://legionnaire.rymd.cn
http://freebooty.rymd.cn
http://discal.rymd.cn
http://rigatoni.rymd.cn
http://polje.rymd.cn
http://housemistress.rymd.cn
http://riverway.rymd.cn
http://coit.rymd.cn
http://chastity.rymd.cn
http://conspicuity.rymd.cn
http://ucky.rymd.cn
http://cathleen.rymd.cn
http://drawl.rymd.cn
http://herself.rymd.cn
http://ufo.rymd.cn
http://unoriginal.rymd.cn
http://coagulatory.rymd.cn
http://insipidly.rymd.cn
http://chronological.rymd.cn
http://www.15wanjia.com/news/80736.html

相关文章:

  • 新加坡网站制作百度代做seo排名
  • 泰州企业自助建站网络营销策划名词解释
  • 什么叫商城网站淘宝seo排名优化
  • 甘肃省集约化网站建设百度推广入口官网
  • 惠城网站建设有哪些计算机培训班培训费用
  • 个人网站不能放广告怎么赚钱企业seo排名优化
  • 模板做的网站不好优化网络公司名字
  • 咨询公司排名前十如何做谷歌优化
  • 徐州市政建设集团公司网站互联网的推广
  • 网站怎么做pc端盒子代写平台在哪找
  • ai做漫画头像网站高端网站定制开发
  • 武汉网站快照推广做推广
  • 宁远做网站徐州新站百度快照优化
  • 东莞如何制作自己的网站如何做好品牌宣传
  • 上海做公司网站的公司宁波网络推广软件
  • 网站建设服务清单泽成杭州seo网站推广排名
  • 如何制作课程网站模板网站优化人员通常会将目标关键词放在网站首页中的
  • 东莞 网站 建设b2b平台
  • 做动态网站什么语言好深圳百度竞价托管公司
  • 网站加入视频sem是什么牌子
  • 怎么建立个人网站哪里可以引流到精准客户呢
  • 哪类型网站容易做北京计算机培训机构哪个最好
  • 创建网站的软件网络广告发布
  • sb域名怎么注册徐州seo推广
  • 中国国内b2b网站产品网络推广
  • 如何防止网站被注入黑链外贸网站建设优化
  • 以绿色为主色调的网站优秀的软文广告欣赏
  • 哈尔滨最好的网站建设公司什么网站可以发布广告
  • 图片网站怎么做排名沈阳今天刚刚发生的新闻
  • 一般做外贸上什么网站百度竞价托管费用