mirror of
https://gitee.com/vant-contrib/vant-weapp.git
synced 2025-04-05 19:41:45 +08:00
feat:Uploader 加入上传云存储示例 (#2430)
This commit is contained in:
parent
cd3e75fe14
commit
d4f6406390
@ -9,7 +9,9 @@ Page({
|
||||
],
|
||||
fileList3: [{ url: 'https://img.yzcdn.cn/vant/sand.jpg' }],
|
||||
fileList4: [],
|
||||
fileList5: []
|
||||
fileList5: [],
|
||||
fileList6: [],
|
||||
cloudPath: []
|
||||
},
|
||||
|
||||
beforeRead(event) {
|
||||
@ -40,5 +42,34 @@ Page({
|
||||
this.setData({ [`fileList${name}`]: fileList });
|
||||
},
|
||||
|
||||
clickPreview() {}
|
||||
clickPreview() {},
|
||||
|
||||
uploadToCloud() {
|
||||
wx.cloud.init();
|
||||
const { fileList6: fileList = [] } = this.data;
|
||||
if (!fileList.length) {
|
||||
wx.showToast({ title: '请选择图片', icon: 'none' });
|
||||
} else {
|
||||
const uploadTasks = fileList.map((file, index) =>
|
||||
this.uploadFilePromise(`my-photo${index}.png`, file)
|
||||
);
|
||||
Promise.all(uploadTasks)
|
||||
.then(data => {
|
||||
wx.showToast({ title: '上传成功', icon: 'none' });
|
||||
const fileList = data.map(item => ({ url: item.fileID }));
|
||||
this.setData({ cloudPath: data, fileList6: fileList });
|
||||
})
|
||||
.catch(e => {
|
||||
wx.showToast({ title: '上传失败', icon: 'none' });
|
||||
console.log(e);
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
uploadFilePromise(fileName, chooseResult) {
|
||||
return wx.cloud.uploadFile({
|
||||
cloudPath: fileName,
|
||||
filePath: chooseResult.path
|
||||
});
|
||||
}
|
||||
});
|
||||
|
@ -56,3 +56,18 @@
|
||||
use-before-read="{{ true }}"
|
||||
/>
|
||||
</demo-block>
|
||||
|
||||
<demo-block title="云存储上传" padding>
|
||||
<van-uploader
|
||||
name="6"
|
||||
file-list="{{ fileList6 }}"
|
||||
bind:before-read="beforeRead"
|
||||
bind:after-read="afterRead"
|
||||
bind:delete="delete"
|
||||
bind:click-preview="clickPreview"
|
||||
use-before-read="{{ true }}"
|
||||
/>
|
||||
<view class="demo-margin-bottom">
|
||||
<van-button type="primary" bind:click="uploadToCloud">上传至云存储</van-button>
|
||||
</view>
|
||||
</demo-block>
|
@ -39,8 +39,8 @@ Page({
|
||||
url: 'https://example.weixin.qq.com/upload', // 仅为示例,非真实的接口地址
|
||||
filePath: file.path,
|
||||
name: 'file',
|
||||
formData: { 'user': 'test' },
|
||||
success (res){
|
||||
formData: { user: 'test' },
|
||||
success(res) {
|
||||
// 上传完成需要更新 fileList
|
||||
const { fileList = [] } = this.data;
|
||||
fileList.push({ ...file, url: res.data });
|
||||
@ -67,7 +67,11 @@ Page({
|
||||
{ url: 'https://img.yzcdn.cn/vant/leaf.jpg', name: '图片1' },
|
||||
// Uploader 根据文件后缀来判断是否为图片文件
|
||||
// 如果图片 URL 中不包含类型信息,可以添加 isImage 标记来声明
|
||||
{ url: 'http://iph.href.lu/60x60?text=default', name: '图片2', isImage: true }
|
||||
{
|
||||
url: 'http://iph.href.lu/60x60?text=default',
|
||||
name: '图片2',
|
||||
isImage: true
|
||||
}
|
||||
]
|
||||
}
|
||||
});
|
||||
@ -123,6 +127,41 @@ Page({
|
||||
});
|
||||
```
|
||||
|
||||
### 上传图片至云存储
|
||||
|
||||
在开发中,可以利用小程序的云存储能力,将图片上传至云存储内。然后根据返回的 f`ileiId`来下载图片、删除图片和替换临时链接。
|
||||
|
||||
```js
|
||||
// 上传图片
|
||||
uploadToCloud() {
|
||||
wx.cloud.init();
|
||||
const { fileList } = this.data;
|
||||
if (!fileList.length) {
|
||||
wx.showToast({ title: '请选择图片', icon: 'none' });
|
||||
} else {
|
||||
const uploadTasks = fileList.map((file, index) => this.uploadFilePromise(`my-photo${index}.png`, file));
|
||||
Promise.all(uploadTasks)
|
||||
.then(data => {
|
||||
wx.showToast({ title: '上传成功', icon: 'none' });
|
||||
const newFileList = data.map(item => { url: item.fileID });
|
||||
this.setData({ cloudPath: data, fileList: newFileList });
|
||||
})
|
||||
.catch(e => {
|
||||
wx.showToast({ title: '上传失败', icon: 'none' });
|
||||
console.log(e);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
uploadFilePromise(fileName, chooseResult) {
|
||||
return wx.cloud.uploadFile({
|
||||
cloudPath: fileName,
|
||||
filePath: chooseResult.path
|
||||
});
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### Props
|
||||
|
||||
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|
||||
@ -141,7 +180,6 @@ Page({
|
||||
| max-count | 文件上传数量限制 | *number* | - | - |
|
||||
| upload-text | 上传区域文字提示 | *string* | - | - |
|
||||
| image-fit | 预览图裁剪模式,可选值参考小程序`image`组件的`mode`属性 | *string* | `scaleToFill` | - |
|
||||
|
||||
### Slot
|
||||
|
||||
| 名称 | 说明 |
|
||||
@ -156,4 +194,4 @@ Page({
|
||||
| bind:after-read | 文件读取完成后的回调函数 | `event.detail.file`: 当前读取的文件 |
|
||||
| bind:oversize | 文件超出大小限制的回调函数 | - |
|
||||
| bind:click-preview | 点击预览图片的回调函数 | `event.detail.index`: 点击图片的序号值 |
|
||||
| bind:delete | 删除图片的回调函数 | `event.detail.index`: 删除图片的序号值 |
|
||||
| bind:delete | 删除图片的回调函数 | `event.detail.index`: 删除图片的序号值 |
|
Loading…
x
Reference in New Issue
Block a user