Get the App
SLTechnology News&Howtos  ›  Development  › 

How to encapsulate multiple pictures and upload api in WeChat Mini Programs

Shulou Source: shulou.com Published: 2022-05-31 22:59:27 09月20日 Update

This article introduces the relevant knowledge of "how to encapsulate multiple pictures to upload api" in WeChat Mini Programs. In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The code is as follows

Export default class Upload {constructor (object) {this.obj = {count:1, sizeType: ['original',' compressed'], / / you can specify whether it is the original image or a compressed image. By default, both have sourceType: ['album','camera'], / / you can specify whether the source is a photo album or a camera By default, both have} if (Object.prototype.toString.call (object) = = "[object Object]") {Object.assign (this.obj, object) } else {uni.showToast ({title: 'parameter must be an object', icon: "icon", duration: 2000});} return this } / / an array collection of pictures returned by uploading pictures async uploadPic () {let chooseImageResult = await this.chooseImage () console.log ("Select picture", chooseImageResult) let imgArr = await chooseImageResult.tempFilePaths.map (async (item,index) = > {uni.showLoading ({title: `uploading the first ${index+1} `}) Let uploadFileResult = await this.uploadFile (item) console.log ("upload process", uploadFileResult) return getApp (). GlobalData.img_prefix + uploadFileResult.data.file.url;}) return new Promise ((resolve,reject) = > {Promise.all (imgArr). Then ((result) = > {uni.hideLoading () Uni.showToast ({title: 'upload successful', icon: "none", duration: 2000}) Console.log ("upload result", result) resolve (result)})} uploadFile (file) {return new Promise ((resolve, reject) = > {uni.uploadFile ({url: 'https://baidu.com/upload/', / / here is your own upload interface filePath: file, name:' file' Success: function (res) {var data = res.data Resolve (JSON.parse (data))}, fail: function (res) {reject (upload failed)}, complete: function (res) {uni.hideToast () }})} chooseImage () {return new Promise ((resolve,reject) = > {uni.chooseImage ({count: this.obj.count,//1, / / default 9 sizeType: this.obj.sizeType,// ['original',' compressed'], / / you can specify whether it is the original image or the compressed image By default, both have sourceType: this.obj.sourceType,// ['album','camera'], / / you can specify whether the source is a photo album or a camera. By default, both have success: function (res) {/ / console.log (res) resolve (res)}, fail:function () {reject ("failed to select a file")}

Use an example

Let object = {count:1, sizeType: ['original',' compressed'], / / you can specify whether it is the original image or a compressed image. By default, both have sourceType: ['album','camera'], / / you can specify whether the source is a photo album or a camera, and both have} let result = await new Upload (object). UploadPic () This is the end of the content of "how to encapsulate multiple pictures and upload api in WeChat Mini Programs". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Pictures or original pictures sources photo albums cameras multiple pictures programs encapsulation content more knowledge process selection practicality success learning and then code parameters Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Huawei Linux MariaDB OPPO Reno