首先到阿里云下載文檔示例
解壓后里面有個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
- })
- }
- })
- },
新聞熱點
疑難解答