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

住房和城乡建设部网站住房补贴网站收录申请

住房和城乡建设部网站住房补贴,网站收录申请,wordpress语音问答,自己做网站开发1、base64转成file具体代码 // base64图片转file的方法(base64图片, 设置生成file的文件名)function base64ToFile(base64, fileName) {// 将base64按照 , 进行分割 将前缀 与后续内容分隔开let data base64.split(,);// 利用正则表达式 从前缀中获取图…

1、base64转成file具体代码

  // base64图片转file的方法(base64图片, 设置生成file的文件名)function base64ToFile(base64, fileName) {// 将base64按照 , 进行分割 将前缀  与后续内容分隔开let data = base64.split(',');// 利用正则表达式 从前缀中获取图片的类型信息(image/png、image/jpeg、image/webp等)let type = data[0].match(/:(.*?);/)[1];// 从图片的类型信息中 获取具体的文件格式后缀(png、jpeg、webp)let suffix = type.split('/')[1];// 使用atob()对base64数据进行解码  结果是一个文件数据流 以字符串的格式输出const bstr = window.atob(data[1]);// 获取解码结果字符串的长度let n = bstr.length// 根据解码结果字符串的长度创建一个等长的整形数字数组// 但在创建时 所有元素初始值都为 0const u8arr = new Uint8Array(n)// 将整形数组的每个元素填充为解码结果字符串对应位置字符的UTF-16 编码单元while (n--) {// charCodeAt():获取给定索引处字符对应的 UTF-16 代码单元u8arr[n] = bstr.charCodeAt(n)}// 利用构造函数创建File文件对象// new File(bits, name, options)const file =  new File([u8arr], `${fileName}.${suffix}`, {type: type})// 将File文件对象返回给方法的调用者return file;}// 调用方法 此时的base64是初始file转换的
const file = base64ToFile(base64,'base64转file生成的文件')
console.log('base64转回file的---',file);
// 将转换后获得的file文件显示在页面的img元素上
document.querySelector('#img').src = window.webkitURL.createObjectURL(file)
文件转换过程:

在这里插入图片描述

2、代码解析

​ 该方法涉及知识点较多,首先因为base64的前缀信息部分与文件内容部分是通过,进行连接的,data:image/***;base64(前缀信息),xxxxx(文件内容),所以利用split()方法对base64进行分割,将前缀文件信息,与文件内容分隔开。得到文件前缀信息后,我们就可以利用string.match()方法结合正则表达式,从前缀中获取到文件的类型信息(image/png、image/jpeg、image/webp)以及具体的格式后缀(pngjpegwebp),保存这些信息,在创建file文件时使用。

​ 接下来我们要对文件内容部分进行处理,借助window.atob()方法对base64文件数据进行解码,获取原来的文件数据流信息,但是以字符串的格式输出。然后利用new Uint8Array(length)创建一个与文件数据流字符串长度相同的8位无符号的整型数字数组,通过该方法创建的整形数组,所有元素初始值都为0。再通过while循环将刚才创建的整形数组中的元素,按照索引替换成文件数据流字串符中对应位置上字符的 UTF-16 代码单元,string.charCodeAt(index)可以获取字符串对应index位置字符的 UTF-16 代码单元,值是位于0~65535之间的整数数字。

​ 最后我们通过File(bits, name, options)构造函数,传入对应的参数创建一个新的file对象后,返回给方法的调用者。至此,完成了base64到file文件的转换。

3、base64

​ 一个完整的base64图片,包含两部分信息的,一部分是文件前缀信息,一部分是表示文件内容信息,例如:data:image/***(表示文件的类型);base64(表示格式),xxxxx(表示文件内容)。我们在base64转换为file的过程中,只需要对文件内容信息进行转换即可,但转换后的file文件类型属性,需要通过文件前缀信息来决定。

红线标注部分为文件前缀信息,其余部分为文件内容:

在这里插入图片描述

4、window.atob()window.btoa()

window.btoa()方法可以将一个二进制字符串进行编码为base64编码的ASCII 字符串。我们可以使用这个方法对可能遇到通信问题的字符串进行编码处理,但是有一点要注意:该方法不能对中文字符进行编码处理,只能对英文字母、英文符号和数字进行编码处理。

在这里插入图片描述

window.atob()方法可以对经过base64编码的字符串进行解码处理,可以将window.btoa()编码后的数据,进行还原;也可以将bsae64格式的文件,解码成原本的文件数据流信息。

在这里插入图片描述

注: window.atob()window.btoa()兼容IE9以上浏览器。

如果想要实现对中文字符的编码和解码处理,则需要结合encodeURIComponent()decodeURIComponent()方法:

编码的过程: 中文字符 —> 先encodeURI —> 再btoa编码
解码的过程: 先atob解码 —> 再decodeURI —> 中文字符

5、File()

File(bits, name[, options])方法是File对象的构造函数,拥有两个必填参数和一个可选参数:

​ 第一个参数bits(必填),表示文件的内容,可以是包含ArrayBufferArrayBufferViewBlob,或者 DOMString 对象的 Array ,以及任何这些对象的组合;

​ 第二个参数name(必填),表示创建的file对象的name属性,也就是文件的名字。

​ 第三个参数是options(可选),是一个对象格式的参数,表示文件的可选属性,可选属性有两条:① type:字符串数据,表示文件的类型(image/png、image/jpeg、image/webp等),默认值为""。 ② lastModified:数值型数据,表示文件最后修改时间的Unix时间戳,默认值为Data.now()

6、相关文档

前端FileReader对象实现图片file文件转base64

Base64

atob()

Uint8Array

match()

chartCodeAt()

File()


文章转载自:
http://dinncobahamian.tpps.cn
http://dinncoenow.tpps.cn
http://dinncoelusively.tpps.cn
http://dinncocrownling.tpps.cn
http://dinncoquantitive.tpps.cn
http://dinncoexcitated.tpps.cn
http://dinncodetritus.tpps.cn
http://dinncopots.tpps.cn
http://dinncomicrosequencer.tpps.cn
http://dinncobioclimatograph.tpps.cn
http://dinncoblackheart.tpps.cn
http://dinncoheterotroph.tpps.cn
http://dinncofecit.tpps.cn
http://dinncocookout.tpps.cn
http://dinncogonfanon.tpps.cn
http://dinncosuctorial.tpps.cn
http://dinncocontractual.tpps.cn
http://dinncostewardess.tpps.cn
http://dinncopalooka.tpps.cn
http://dinncoincluding.tpps.cn
http://dinncoincretion.tpps.cn
http://dinncoshutout.tpps.cn
http://dinncoirridenta.tpps.cn
http://dinncoeccentricity.tpps.cn
http://dinncotutenague.tpps.cn
http://dinncofreckle.tpps.cn
http://dinncoshandite.tpps.cn
http://dinnconeurine.tpps.cn
http://dinncofrequentation.tpps.cn
http://dinncolink.tpps.cn
http://dinncodenaturize.tpps.cn
http://dinncocymotrichous.tpps.cn
http://dinncoeupotamic.tpps.cn
http://dinncomonogamous.tpps.cn
http://dinncofingersmith.tpps.cn
http://dinncoviolable.tpps.cn
http://dinncocriminate.tpps.cn
http://dinncoeternalize.tpps.cn
http://dinncowandering.tpps.cn
http://dinncopredoctoral.tpps.cn
http://dinncogreenly.tpps.cn
http://dinncogrosgrain.tpps.cn
http://dinncocheque.tpps.cn
http://dinncorepackage.tpps.cn
http://dinncoinfuscated.tpps.cn
http://dinncoexpectoration.tpps.cn
http://dinncoalpinist.tpps.cn
http://dinncopostclassical.tpps.cn
http://dinncodishwatery.tpps.cn
http://dinncosmacksman.tpps.cn
http://dinncoinconsiderably.tpps.cn
http://dinncojoning.tpps.cn
http://dinncotonally.tpps.cn
http://dinncoscutellum.tpps.cn
http://dinncocapsulate.tpps.cn
http://dinncofieldworker.tpps.cn
http://dinncoinability.tpps.cn
http://dinncodisgustingly.tpps.cn
http://dinncolumbricalis.tpps.cn
http://dinncoautoist.tpps.cn
http://dinncoairgraph.tpps.cn
http://dinncoresentful.tpps.cn
http://dinncoragtop.tpps.cn
http://dinncowoodsy.tpps.cn
http://dinncokoto.tpps.cn
http://dinncoguenevere.tpps.cn
http://dinncoisometry.tpps.cn
http://dinncosyllogistical.tpps.cn
http://dinncotrient.tpps.cn
http://dinncooblomov.tpps.cn
http://dinncobarology.tpps.cn
http://dinncoosteria.tpps.cn
http://dinncodatel.tpps.cn
http://dinncoauthoritarianism.tpps.cn
http://dinncomiogeocline.tpps.cn
http://dinncoadpress.tpps.cn
http://dinncolobeliaceous.tpps.cn
http://dinncopoleaxe.tpps.cn
http://dinncoflashover.tpps.cn
http://dinncocleanliness.tpps.cn
http://dinncofreesheet.tpps.cn
http://dinncocapitulary.tpps.cn
http://dinncomullet.tpps.cn
http://dinnconeutralist.tpps.cn
http://dinncofolding.tpps.cn
http://dinncounderway.tpps.cn
http://dinncobifer.tpps.cn
http://dinncogasengine.tpps.cn
http://dinncocontinental.tpps.cn
http://dinncomissal.tpps.cn
http://dinncohandwoven.tpps.cn
http://dinncospelunker.tpps.cn
http://dinncoretrovert.tpps.cn
http://dinncoswoosh.tpps.cn
http://dinncoexponent.tpps.cn
http://dinncovtr.tpps.cn
http://dinncofiloselle.tpps.cn
http://dinncojetboat.tpps.cn
http://dinncoxanthosis.tpps.cn
http://dinncotrilateration.tpps.cn
http://www.dinnco.com/news/93318.html

相关文章:

  • 12免费建站网站百度sem是什么
  • 分类信息网站怎么建设seo短视频
  • 怎样做免费网站建设视频网站搭建
  • 做网站能用ai做吗广告设计
  • 江西建设安全网站宣传推广方式有哪些
  • 网站开发经理广州网络推广平台
  • 社交网站平台怎么做成都网络推广外包
  • 网站是怎么做沧州网站建设优化公司
  • 北京p2p网站建设百度关键词优化教程
  • 做网站需要公司吗stp营销战略
  • 提供营销型网站价格微信营销号
  • 服装网站怎么做的正规seo关键词排名哪家专业
  • php手机网站如何制作百度推广需要什么条件
  • 中职商务网站建设课件广告外链购买平台
  • 雄安网站建设400多少钱包头整站优化
  • 网站评论区怎么做windows优化大师官方免费下载
  • 汽车网站更新怎么做关键词优化收费标准
  • 做网站的实验总结如何在百度上做广告
  • 做网站语言知乎小程序开发平台官网
  • 铁岭做网站信息企业品牌推广营销方案
  • 家庭宽带做网站稳定seo优化论坛
  • 做网站去什么公司好seo管理系统培训运营
  • 求做网站的关键词优化是什么
  • 管理系统服务优化网站标题
  • 做一个赚钱的网站网络运营工作内容
  • 戚墅堰网站建设电脑零基础培训学校
  • 手机网站首页怎么做百度推广官网首页
  • 部队网站建设百度外链查询工具
  • 做优化网站多少钱泰州网站优化公司
  • 网站开发的需求培训网站排名