当前位置: 首页>編程日記>正文

小程序图片上传formdata boundary + base64

小程序图片上传formdata boundary + base64


1、使用 boundary


因为小程序上传图片只能单图上产,想着上传图片的时候直接for循环搞上去,谁知后台要一个文件类型(boundary),就这样头疼的搞了一中午,直接先看效果吧
![在这里插入图片描述](https://img-blog.csdnimg.cn/2021070516200750.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzA5MDAxOA==,size_16,color在这里插入图片描述
在这里插入图片描述

小程序只提供了wx.requestwx.uploadFile这两种post请求,在我想。request中只需要将data变为ArrayBuffer类型就可以了

multipart/form-data的数据格式是这个样子的

在这里插入图片描述

数据body被Boundary分隔成几部分,Boundary的值为content-type里的boundary。
每部分包含Content-Disposition,name,filename,换行符\r\n,Content-Type和内容
最后以’–’+Boundary+’–'结尾

1、生成随机分隔符

 //生成分割字符串generateDivisionStr() {return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {let r = Math.random() * 16 | 0,v = c == 'x' ? r : (r & 0x3 | 0x8)return v.toString(16);})},

2、封装上传所需参数

generateUploadParams(str,obj) {let pra = ''Object.keys(obj).forEach(key => {//如果是ArrayBuffer类型的图片文件if(typeof obj[key] === "object"){//随机图片文件名let iName = this.generateDivisionStr()pra += `\r\n--${str}` +`\r\nContent-Disposition: form-data; name="${key}";filename="${iName}.png"` +`\r\nContent-Type: "image/png"`+`\r\n` +`\r\n${obj[key]}` }else{pra += `\r\n--${str}` +`\r\nContent-Disposition: form-data; name="${key}"` +`\r\n` +`\r\n${obj[key]}` }})pra = pra + `\r\n--${str}--`console.log(pra)return pra},

3、使用方法
1、当选择图片的时候
bn:就是处理后的图片

//上传图片async handleUploadImg(e) {let that = thisconst fs = wx.getFileSystemManager()const {type} = e.currentTarget.datasetlet fp = app.globalData.uploadImgslet res = await Util.uploadImages(1, type)this.setData({[type]: res.join('').replace(/\s*/g, "")})console.log(fp)fs.readFile({filePath: fp[0].file[0], //要读取的文件的路径 如果不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容success(res) {console.log(res.data)that.bn = res.datathat.setData({bn: res.data})}})},

2、点击提交的时候提交输入的数据和图片

onClickVerify() {const obj = {"ownerInfo.name": 111,"ownerInfo.identityNo": 222,"imgs[0].file": this.bn,"imgs[0].type": 1}//这里的分隔符一定要是同一个let str = this.generateDivisionStr()let data = this.generateUploadParams(str,obj)Util.request({url: "http://192.168.18.43:18930/ebikeOrderInfo/uploadData",method: 'POST',header: {'content-type': `multipart/form-data; boundary=${str}`},data,success: function (res) {},})
}

在这里插入图片描述


2、使用base64


多图上传的时候我们也可以将图片转为base64给后台,让后台再将base64转换为图片,这样就不用微信的限制,一次只能传一个

utils.js(其他封装的就不写了)

class Util {type为图片类型,因为项目中要对不同的图片进行标识,并传给后台static uploadImages(count, type) {let app = getApp()let flag = truelet maxSize = 1024 * 1024// 1:编码   2:后方45°   3:证反面   4:证正面    5:车牌     6:拓印const imgType = {'carCodeImg': 1,'carAngleImg': 2,'cardPositiveImg': 3,'cardSideImg': 4,'carImages': 5,'rubbingImg': 6}return new Promise((resolve, reject) => {wx.chooseImage({count: count || 1,success:(res) => {for (let i = 0; i < res.tempFiles.length; i++) {const fs = wx.getFileSystemManager()fs.readFile({encoding: "base64",filePath: res.tempFiles[i].path,success(val) {wx.getImageInfo({src: res.tempFiles[i].path,success(info) {// resolve(res.tempFilePaths)app.globalData.uploadImgs.push({file: res.tempFilePaths,type: imgType[type],imgTypes: info.type,type,imgUrl: res.tempFilePaths.join('').replace(/\s*/g, ""),imgBoundary: `data:image/${info.type};base64,${val.data}`})resolve(app.globalData.uploadImgs)}})}})}},fail(err) {reject(err)}})})}
}module.exports = Util

在需要用到的页面引入后

async handleUploadImg(e) {const { type } = e.currentTarget.datasetlet res = await Util.uploadImages(1, type)for (let i = 0; i < res.length; i++) {let tType = res[i].typethis.setData({[tType]: {imgUrl: res[i].imgUrl,imgBoundary: res[i].imgBoundary}})}

在这里插入图片描述


https://www.fengoutiyan.com/post/16171.html

相关文章:

  • 微信做图片的小程序
  • 微信小程序编辑图片
  • 微信小程序图片设置
  • 小程序怎么上传图片流程
  • 图片小程序
  • 微信小程序图片上传显示
  • 前端formdata文件上传
  • vue formdata上传文件
  • 鏡像模式如何設置在哪,圖片鏡像操作
  • 什么軟件可以把圖片鏡像翻轉,C#圖片處理 解決左右鏡像相反(旋轉圖片)
  • 手機照片鏡像翻轉,C#圖像鏡像
  • 視頻鏡像翻轉軟件,python圖片鏡像翻轉_python中鏡像實現方法
  • 什么軟件可以把圖片鏡像翻轉,利用PS實現圖片的鏡像處理
  • 照片鏡像翻轉app,java實現圖片鏡像翻轉
  • 什么軟件可以把圖片鏡像翻轉,python圖片鏡像翻轉_python圖像處理之鏡像實現方法
  • matlab下載,matlab如何鏡像處理圖片,matlab實現圖像鏡像
  • 圖片鏡像翻轉,MATLAB:鏡像圖片
  • 鏡像翻轉圖片的軟件,圖像處理:實現圖片鏡像(基于python)
  • canvas可畫,JavaScript - canvas - 鏡像圖片
  • 圖片鏡像翻轉,UGUI優化:使用鏡像圖片
  • Codeforces,CodeForces 1253C
  • MySQL下載安裝,Mysql ERROR: 1253 解決方法
  • 勝利大逃亡英雄逃亡方案,HDU - 1253 勝利大逃亡 BFS
  • 大一c語言期末考試試題及答案匯總,電大計算機C語言1253,1253《C語言程序設計》電大期末精彩試題及其問題詳解
  • lu求解線性方程組,P1253 [yLOI2018] 扶蘇的問題 (線段樹)
  • c語言程序設計基礎題庫,1253號C語言程序設計試題,2016年1月試卷號1253C語言程序設計A.pdf
  • 信奧賽一本通官網,【信奧賽一本通】1253:抓住那頭牛(詳細代碼)
  • c語言程序設計1253,1253c語言程序設計a(2010年1月)
  • 勝利大逃亡英雄逃亡方案,BFS——1253 勝利大逃亡
  • 直流電壓測量模塊,IM1253B交直流電能計量模塊(艾銳達光電)
  • c語言程序設計第三版課后答案,【渝粵題庫】國家開放大學2021春1253C語言程序設計答案
  • 18轉換為二進制,1253. 將數字轉換為16進制
  • light-emitting diode,LightOJ-1253 Misere Nim
  • masterroyale魔改版,1253 Dungeon Master
  • codeformer官網中文版,codeforces.1253 B
  • c語言程序設計考研真題及答案,2020C語言程序設計1253,1253計算機科學與技術專業C語言程序設計A科目2020年09月國家開 放大學(中央廣播電視大學)
  • c語言程序設計基礎題庫,1253本科2016c語言程序設計試題,1253電大《C語言程序設計A》試題和答案200901
  • 肇事逃逸車輛無法聯系到車主怎么辦,1253尋找肇事司機