miniprogram-demo/packageAPI/pages/upload-file/upload-file.js

39 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2020-12-09 15:33:16 +08:00
// pages/API/upload-file/upload-file.js
Page({
chooseImage() {
const self = this
wx.chooseImage({
success (res) {
const tempFilePaths = res.tempFilePaths
const files = res.tempFiles;
wx.uploadFile({
2020-12-09 16:15:33 +08:00
url: 'https://finchat-mop.finogeeks.club/api/v1/netdisk/upload/self?type=file&content={}', //仅为示例,非真实的接口地址
2020-12-09 15:33:16 +08:00
filePath: tempFilePaths[0],
name: 'file',
2020-12-09 16:15:33 +08:00
header: {
'X-Consumer-Custom-ID': 'test',
2020-12-09 15:33:16 +08:00
},
2020-12-09 16:15:33 +08:00
success: res => {
// 返回文件 ID
2021-06-17 15:42:44 +08:00
console.log('uploadImage success, res is:', res)
const data = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
2020-12-09 16:15:33 +08:00
const url = `https://finchat-mop.finogeeks.club/api/v1/mop/netdisk/download/${data.netdiskID}`
self.setData({
imageSrc: url
});
wx.showToast({
title: '上传成功',
icon: 'success',
duration: 1000
})
2020-12-09 15:33:16 +08:00
},
2020-12-09 16:15:33 +08:00
fail({errMsg}) {
console.log('uploadImage fail, errMsg is', errMsg)
2020-12-09 15:33:16 +08:00
}
})
}
})
},
})