首先到阿里云下载文档示例
解压后里面有个upload.js文件,编辑这个文件,把accessid、accesskey、host换成自己的,由于小程序只接受https,所以host最好是绑定自己的域名,且实用了https,然后找到new_multipart_params,使用log打印出来,接下来运行一遍index.html,上传图片后会打印出我们需要的东西
接下来就小程序上传了,使用
chooseImage和uploadFile
uploadImage: function () { var that = this wx.chooseImage({ count: 3, // 默认9 sizeType: ['original'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 success: function (res) { // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片 var tempFilePaths = res.tempFilePaths var myDate = new Date() var ossPath = 'seekings/' + myDate.getFullYear() for (var i = 0; i < tempFilePaths.length; i++) { // 获取文件后缀 var pathArr = tempFilePaths[i].split('.') // 随机生成文件名称 var fileRandName = Date.now() + "" + parseInt(Math.random() * 1000) var fileName = fileRandName + '.' + pathArr[3] // 要提交的key var fileKey = ossPath + '/' + fileName wx.uploadFile({ url: 'https://xxx.xxx.com', filePath: tempFilePaths[i], name: 'file', formData: { name: tempFilePaths[i], key: fileKey, policy: 'xxxxxxxxxx', OSSAccessKeyId: 'xxxxxxx', signature: 'xxxxxx', success_action_status: "200" }, success: function (res) { var data = res.data console.log(res) } }) } that.setData({ upliadImages: res.tempFilePaths }) } }) },