欢迎来到258分享网,纯净的网络源码分享基地!

258资源分享网

全部作品
全部作品
网站源码
微信源码
素材特效
源码插件
视频教程
建站学院
热门搜索: 织梦  农业种植  农业  安全设置  官方
258资源分享 > 建站学院 > 微信开发 > 微信小程序上传一或多张图片

推荐下载

HTML5响应式自适应网咯设计

2020-05-12   浏览:736

高端HTML5响应式企业通用网

2020-05-06   浏览:517

html5响应式外贸网站英文版

2020-05-08   浏览:505

HTML5自适应律师工作室类网

2020-04-04   浏览:501

HTML5影视传媒文化公司类网

2020-05-12   浏览:495

微信小程序上传一或多张图片

发布时间:2020-11-20  

一.要点

1.选取图片

 

wx.chooseImage({

sizeType: [], // original 原图,compressed 压缩图,默认二者都有

sourceType: [], // album 从相册选图,camera 使用相机,默认二者都有

success: function (res) {

console.log(res);

var array = res.tempFilePaths, //图片的本地文件路径列表

}

})

2.上传图片

 

wx.uploadFile({

url: '', //开发者服务器的 url

filePath: '', // 要上传文件资源的路径 String类型!!!

name: 'uploadFile', // 文件对应的 key ,(后台接口规定的关于图片的请求参数)

header: {

'content-type': 'multipart/form-data'

}, // 设置请求的 header

formData: { }, // HTTP 请求中其他额外的参数

success: function (res) {

},

fail: function (res) {

}

})

二.代码示例

 

// 点击上传图片

upShopLogo: function () {

var that = this;

wx.showActionSheet({

itemList: ['从相册中选择', '拍照'],

itemColor: "#f7982a",

success: function (res) {

if (!res.cancel) {

if (res.tapIndex == 0) {

that.chooseWxImageShop('album')

} else if (res.tapIndex == 1) {

that.chooseWxImageShop('camera')

}

}

}

})

},

chooseWxImageShop: function (type) {

var that = this;

wx.chooseImage({

sizeType: ['original', 'compressed'],

sourceType: [type],

success: function (res) {

 

/*上传单张

that.data.orderDetail.shopImage = res.tempFilePaths[0],

that.upload_file(API_URL + 'shop/shopIcon', res.tempFilePaths[0])

*/

 

/*上传多张(遍历数组,一次传一张)

for (var index in res.tempFilePaths) {

that.upload_file(API_URL + 'shop/shopImage', res.tempFilePaths[index])

}

*/

}

})

},

upload_file: function (url, filePath) {

var that = this;

wx.uploadFile({

url: url,

filePath: filePath,

name: 'uploadFile',

header: {

'content-type': 'multipart/form-data'

}, // 设置请求的 header

formData: { 'shopId': wx.getStorageSync('shopId') }, // HTTP 请求中其他额外的 form data

success: function (res) {

wx.showToast({

title: "图片修改成功",

icon: 'success',

duration: 700

})

},

fail: function (res) {

}

本文标签

:备案管家服务帮您把复杂流程变简单,解决您在进行网站备案过程中遇到的效率低下和成功率不高的问题。个人300/2个域名,企业500/5个域名,qq1650004.