小程序代码
index.wxml
<view>
<button bindtap="upload">点击上传封面</button>
</view>
index.js
/上传图片
// 上传照片
upload: function () {
//选择图片
wx.chooseImage({
count: 1,
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success: function (res) {
wx.showLoading({
title: '上传中',
})
//获取图片临时地址
const filePath = res.tempFilePaths[0]
// 自定义云端的图片名称
const fileName = Math.floor(Math.random() * 1000000) + filePath.match(/.[^.]+?$/)[0]
console.log(fileName);
// 上传到服务器存储空间
var uploadTask=wx.uploadFile({
url: 'http://localhost:8080/imgUpload?fileName='+fileName,