文件上传新方式 files 对象创建

时间:2021-02-03 10:40:28   收藏:0   阅读:0
    
     // 读取文件base64 编码方式
     var readerRes = fs.readFileSync(rp + ‘ext_blocks\\userLib\\‘ + sessionStorage.checkLib + ".zip", "Base64") function base64toBlob(base64, type) { // 将base64转为Unicode规则编码,将头部文件类型做截取 let bstr = atob(base64.substring(base64.indexOf(‘,‘) + 1), type), n = bstr.length, u8arr = new Uint8Array(n); while (n--) { u8arr[n] = bstr.charCodeAt(n) // 转换编码后才可以使用charCodeAt 找到Unicode编码 } return new Blob([u8arr], { type, }) } var blob = base64toBlob(readerRes, "Base64"); // 转为blob 二进制流形式
      // 创建可以上传的files对象
var myFile = new File([blob], sessionStorage.checkLib + ".zip", { lastModified: new Date(), });

     // 以formdata形式上传 var formdata = new FormData() formdata.append("files", myFile)

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!