Upload 上传

用于文件选择或相机拍照上传。

使用指南

在 page.json 中引入组件

  1. {
  2. "navigationBarTitleText": "Upload",
  3. "usingComponents": {
  4. "wux-upload": "../../dist/upload/index"
  5. }
  6. }

示例

  1. <view class="page">
  2. <view class="page__hd">
  3. <view class="page__title">Upload</view>
  4. <view class="page__desc">上传</view>
  5. </view>
  6. <view class="page__bd page__bd_spacing">
  7. <view class="sub-title">Default</view>
  8. <wux-upload url="https://www.skyvow.cn/api/common/file" bind:change="onChange" bind:success="onSuccess" bind:fail="onFail" bind:complete="onComplete">
  9. <button type="default">Click to Upload</button>
  10. </wux-upload>
  11. <view class="sub-title">Disabled</view>
  12. <wux-upload disabled url="https://www.skyvow.cn/api/common/file" bind:change="onChange" bind:success="onSuccess" bind:fail="onFail" bind:complete="onComplete">
  13. <button disabled type="default">Click to Upload</button>
  14. </wux-upload>
  15. <view class="sub-title">Picture card & max = 4</view>
  16. <wux-upload listType="picture-card" defaultFileList="{{ fileList }}" max="4" url="https://www.skyvow.cn/api/common/file" bind:change="onChange" bind:success="onSuccess" bind:fail="onFail" bind:complete="onComplete" bind:preview="onPreview">
  17. <text>Upload</text>
  18. </wux-upload>
  19. <view class="sub-title">ShowUploadList = false</view>
  20. <wux-upload listType="picture-card" showUploadList="{{ false }}" url="https://www.skyvow.cn/api/common/file" bind:change="onChange" bind:success="onSuccess" bind:fail="onFail" bind:complete="onComplete" bind:preview="onPreview">
  21. <image src="{{ imageUrl }}" wx:if="{{ imageUrl }}" />
  22. <text wx:else>Upload</text>
  23. </wux-upload>
  24. <view class="sub-title">Progress = true</view>
  25. <progress percent="{{ progress }}" show-info />
  26. <wux-upload progress listType="picture-card" showUploadList="{{ false }}" url="https://www.skyvow.cn/api/common/file" bind:change="onChange" bind:success="onSuccess" bind:fail="onFail" bind:complete="onComplete" bind:progress="onProgress" bind:preview="onPreview">
  27. <image src="{{ imageUrl }}" wx:if="{{ imageUrl }}" />
  28. <text wx:else>Upload</text>
  29. </wux-upload>
  30. <view class="sub-title">Remove</view>
  31. <wux-upload listType="picture-card" fileList="{{ fileList }}" controlled url="https://www.skyvow.cn/api/common/file" bind:change="onChange" bind:success="onSuccess" bind:fail="onFail" bind:complete="onComplete" bind:preview="onPreview" bind:remove="onRemove">
  32. <text>Upload</text>
  33. </wux-upload>
  34. </view>
  35. </view>
  1. Page({
  2. data: {
  3. fileList: [{
  4. uid: 0,
  5. status: 'uploading',
  6. url: 'https://wux.cdn.cloverstd.com/qrcode.jpg',
  7. },
  8. {
  9. uid: 1,
  10. status: 'done',
  11. url: 'https://wux.cdn.cloverstd.com/qrcode.jpg',
  12. },
  13. {
  14. uid: 2,
  15. status: 'error',
  16. url: 'https://wux.cdn.cloverstd.com/qrcode.jpg',
  17. }
  18. ],
  19. },
  20. onChange(e) {
  21. console.log('onChange', e)
  22. const { file } = e.detail
  23. if (file.status === 'uploading') {
  24. this.setData({
  25. progress: 0,
  26. })
  27. wx.showLoading()
  28. } else if (file.status === 'done') {
  29. this.setData({
  30. imageUrl: file.url,
  31. })
  32. }
  33. },
  34. onSuccess(e) {
  35. console.log('onSuccess', e)
  36. },
  37. onFail(e) {
  38. console.log('onFail', e)
  39. },
  40. onComplete(e) {
  41. console.log('onComplete', e)
  42. wx.hideLoading()
  43. },
  44. onProgress(e) {
  45. console.log('onProgress', e)
  46. this.setData({
  47. progress: e.detail.file.progress,
  48. })
  49. },
  50. onPreview(e) {
  51. console.log('onPreview', e)
  52. const { file, fileList } = e.detail
  53. wx.previewImage({
  54. current: file.url,
  55. urls: fileList.map((n) => n.url),
  56. })
  57. },
  58. onRemove(e) {
  59. const { file, fileList } = e.detail
  60. wx.showModal({
  61. content: '确定删除?',
  62. success: (res) => {
  63. if (res.confirm) {
  64. this.setData({
  65. fileList: fileList.filter((n) => n.uid !== file.uid),
  66. })
  67. }
  68. },
  69. })
  70. },
  71. })

视频演示

Upload

API

Upload props

参数 类型 描述 默认值
prefixCls string 自定义类名前缀 wux-upload
max number 最大图片张数,设置为 -1 的时候不限制张数 -1
count number 从相册选图时,最多可以选择的图片张数 9
defaultFileType string 上传文件类型,可选值为 image、video image
compressed boolean 是否压缩所选择的视频文件,当 defaultFileTypevideo 时才生效 true
maxDuration number 拍摄视频最长拍摄时间,单位秒。当 defaultFileTypevideo 时才生效 60
camera string 默认拉起的是前置或者后置摄像头。部分 Android 手机下由于系统 ROM 不支持无法生效。当 defaultFileTypevideo 时才生效 back
sizeType array original 原图,compressed 压缩图 [‘original’, ‘compressed’]
sourceType array album 从相册选图,camera 使用相机 [‘album’, ‘camera’]
url string 上传地址 -
name string 文件对应的 key file
header object HTTP 请求 Header, header 中不能设置 Referer {}
formData object HTTP 请求中其他额外的 form data {}
uploaded boolean 默认的上传行为 true
disabled boolean 是否禁用 false
progress boolean 是否监听上传进度变化 false
listType string 上传列表的内建样式,可选值为 text、picture-card text
defaultFileList array 默认已经上传的文件列表,当 controlledfalse 时才生效 []
fileList array 已经上传的文件列表,当 controlledtrue 时才生效 []
controlled boolean 是否受控 说明文档 false
showUploadList boolean 是否展示上传文件列表 true
showRemoveIcon boolean 是否展示删除图标 true
bind:before function 上传文件之前的回调函数 -
bind:change function 上传文件改变时的回调函数 -
bind:success function 上传文件成功时的回调函数 -
bind:fail function 上传文件失败时的回调函数 -
bind:complete function 上传文件完成时的回调函数 -
bind:progress function 监听上传进度变化的回调函数 -
bind:preview function 点击文件时的回调函数 -
bind:remove function 点击删除图标时的回调函数 -

Upload slot

名称 描述
- 自定义内容

Upload externalClasses

名称 描述
wux-class 根节点样式类