Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report