微信小程序封装多张图片上传API,如何改写为长尾词?

2026-04-05 19:361阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计820个文字,预计阅读时间需要4分钟。

微信小程序封装多张图片上传API,如何改写为长尾词?

这篇文章主要介绍了微信小程序封装多张图片上传API代码的实例。文中通过示例代码展示了如何非详细地实现这一功能,对于希望学习和工作的读者具有一定的参考价值。需要的伙伴可以参考以下代码示例:

ex// 示例代码:微信小程序封装多张图片上传APIPage({ data: { images: [] }, // 选择图片 chooseImage: function() { const that=this; wx.chooseImage({ count: 9, // 最多选择9张图片 sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 success: function(res) { // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片 that.setData({ images: that.data.images.concat(res.tempFilePaths) }); } }); }, // 上传图片 uploadImage: function() { const that=this; const images=that.data.images; if (images.length===0) { wx.showToast({ title: '请先选择图片', icon: 'none' }); return; } const uploadTask=[]; for (let i=0; i

这篇文章主要介绍了微信小程序封装多张图片上传api代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

代码如下

export default class Upload{ constructor(object) { this.obj = { count:1, sizeType:['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType:['album','camera'], // 可以指定来源是相册还是相机,默认二者都有 } if(Object.prototype.toString.call(object) === "[object Object]"){ Object.assign(this.obj, object); }else{ uni.showToast({ title: '参数必须为对象', icon:"icon", duration: 2000 }); } return this; } // 上传图片 返回一个图片的数组集合 async uploadPic(){ let chooseImageResult = await this.chooseImage() console.log("选择图片",chooseImageResult) let imgArr = await chooseImageResult.tempFilePaths.map(async (item,index) => { uni.showLoading({ title: `正在上传第${index+1}张` }); let uploadFileResult = await this.uploadFile(item) console.log("上传图片过程",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: '上传成功', icon:"none", duration: 2000 }); console.log("上传图片结果",result) resolve(result) }) }) } uploadFile(file){ return new Promise((resolve, reject) => { uni.uploadFile({ url: 'baidu.com/upload/', //此处是你自己上传接口 filePath: file, name: 'file', success: function (res) { var data = res.data; resolve(JSON.parse(data)) }, fail: function (res) { reject("上传失败") }, complete: function (res) { uni.hideToast(); } }) }) } chooseImage(){ return new Promise((resolve,reject) => { uni.chooseImage({ count: this.obj.count,//1, // 默认9 sizeType: this.obj.sizeType,//['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: this.obj.sourceType,//['album','camera'], // 可以指定来源是相册还是相机,默认二者都有 success: function (res) { // console.log(res) resolve(res) }, fail:function(){ reject("选择文件失败") } }) }) } }

使用实例

let object = { count:1, sizeType:['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType:['album','camera'], // 可以指定来源是相册还是相机,默认二者都有 } let result = await new Upload(object).uploadPic();

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

微信小程序封装多张图片上传API,如何改写为长尾词?

本文共计820个文字,预计阅读时间需要4分钟。

微信小程序封装多张图片上传API,如何改写为长尾词?

这篇文章主要介绍了微信小程序封装多张图片上传API代码的实例。文中通过示例代码展示了如何非详细地实现这一功能,对于希望学习和工作的读者具有一定的参考价值。需要的伙伴可以参考以下代码示例:

ex// 示例代码:微信小程序封装多张图片上传APIPage({ data: { images: [] }, // 选择图片 chooseImage: function() { const that=this; wx.chooseImage({ count: 9, // 最多选择9张图片 sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 success: function(res) { // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片 that.setData({ images: that.data.images.concat(res.tempFilePaths) }); } }); }, // 上传图片 uploadImage: function() { const that=this; const images=that.data.images; if (images.length===0) { wx.showToast({ title: '请先选择图片', icon: 'none' }); return; } const uploadTask=[]; for (let i=0; i

这篇文章主要介绍了微信小程序封装多张图片上传api代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

代码如下

export default class Upload{ constructor(object) { this.obj = { count:1, sizeType:['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType:['album','camera'], // 可以指定来源是相册还是相机,默认二者都有 } if(Object.prototype.toString.call(object) === "[object Object]"){ Object.assign(this.obj, object); }else{ uni.showToast({ title: '参数必须为对象', icon:"icon", duration: 2000 }); } return this; } // 上传图片 返回一个图片的数组集合 async uploadPic(){ let chooseImageResult = await this.chooseImage() console.log("选择图片",chooseImageResult) let imgArr = await chooseImageResult.tempFilePaths.map(async (item,index) => { uni.showLoading({ title: `正在上传第${index+1}张` }); let uploadFileResult = await this.uploadFile(item) console.log("上传图片过程",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: '上传成功', icon:"none", duration: 2000 }); console.log("上传图片结果",result) resolve(result) }) }) } uploadFile(file){ return new Promise((resolve, reject) => { uni.uploadFile({ url: 'baidu.com/upload/', //此处是你自己上传接口 filePath: file, name: 'file', success: function (res) { var data = res.data; resolve(JSON.parse(data)) }, fail: function (res) { reject("上传失败") }, complete: function (res) { uni.hideToast(); } }) }) } chooseImage(){ return new Promise((resolve,reject) => { uni.chooseImage({ count: this.obj.count,//1, // 默认9 sizeType: this.obj.sizeType,//['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: this.obj.sourceType,//['album','camera'], // 可以指定来源是相册还是相机,默认二者都有 success: function (res) { // console.log(res) resolve(res) }, fail:function(){ reject("选择文件失败") } }) }) } }

使用实例

let object = { count:1, sizeType:['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType:['album','camera'], // 可以指定来源是相册还是相机,默认二者都有 } let result = await new Upload(object).uploadPic();

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

微信小程序封装多张图片上传API,如何改写为长尾词?