赞
踩
uni.chooseImage({
success: (chooseImageRes) => {
const tempFilePaths = chooseImageRes.tempFilePaths;
uni.uploadFile({
url: '......',
filePath: tempFilePaths[0],
// name是服务端的属性名
name: 'file',
// 可添加请求头:header: { "Content-Type": "multipart/form-data" },
success: (uploadFileRes) => {
console.log(uploadFileRes.data);
}
});
}
});
这里举例u-view
<u-upload name="filePath" accept="image" :multiple="false" :maxCount="1" @afterRead="afterRead">
<view slot="default">
<view class="function">
从相册选择
</view>
</view>
</u-upload>
async afterRead(event) { wx.uploadFile({ url: '......', header: { "Content-Type": "multipart/form-data" }, filePath: event.file.url, name: event.name, success: async (res) => { // 成功后的操作 }, fail: function (res) { uni.showToast({ icon: "error", title: '服务响应失败' }); } }) },
async saveAvatar() { uni.downloadFile({ url: '......', success: (res) => { if (res.statusCode === 200) { uni.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success: function () { uni.showToast({ title: "保存成功" }); }, fail: function () { uni.showToast({ title: "保存失败", icon: "none" }); } }); } } }) },
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。