doc.md 11.9 KB
Newer Older
L
liuyijun 已提交
1 2 3 4 5 6 7 8
# Uploader 上传

### 介绍

用于将本地的图片或文件上传至服务器。

### 安装

9
``` ts
L
liuyijun 已提交
10
import { Uploader } from '@nutui/nutui-react';
L
liuyijun 已提交
11 12 13
```
### 基本用法

14
:::demo
L
liuyijun 已提交
15
``` tsx
16 17 18 19 20 21 22 23 24 25 26
import React, { useState } from "react";
import { Uploader } from '@nutui/nutui-react';

const App = () => {
  const uploadUrl = 'https://my-json-server.typicode.com/linrufeng/demo/posts'
  const onStart = () => {
    console.log('start 触发')
  }
  return (
    <>
      <h2>基础用法</h2>
O
oasis-cloud 已提交
27
      <Uploader url={uploadUrl} start={onStart} />
28 29 30 31
    </>
  )
}
export default App;
L
liuyijun 已提交
32
```
J
junjun666 已提交
33
:::
L
liuyijun 已提交
34 35 36

### 自定义上传样式

J
junjun666 已提交
37
:::demo
L
liuyijun 已提交
38
``` tsx
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
import React, { useState } from "react";
import { Uploader, Button } from '@nutui/nutui-react';

const App = () => {
  const uploadUrl = 'https://my-json-server.typicode.com/linrufeng/demo/posts'
  return (
    <>
      <h2>自定义上传样式</h2>
      <Uploader url={uploadUrl}>
        <Button type="primary" icon="uploader">
          上传文件
        </Button>
      </Uploader>
    </>
  )
}
export default App;
L
liuyijun 已提交
56
```
J
junjun666 已提交
57
:::
L
liuyijun 已提交
58 59

### 直接调起摄像头(移动端生效)
J
junjun666 已提交
60 61

:::demo
L
liuyijun 已提交
62
``` tsx
63 64 65 66 67 68 69 70
import React, { useState } from "react";
import { Uploader, Button } from '@nutui/nutui-react';

const App = () => {
  const uploadUrl = 'https://my-json-server.typicode.com/linrufeng/demo/posts'
  return (
    <>
      <h2>直接调起摄像头(移动端生效)</h2>
O
oasis-cloud 已提交
71
      <Uploader capture url={uploadUrl} />
72 73 74 75
    </>
  )
}
export default App;
L
liuyijun 已提交
76
```
J
junjun666 已提交
77
:::
L
liuyijun 已提交
78

79
### 上传状态
J
junjun666 已提交
80 81

:::demo
L
liuyijun 已提交
82
``` tsx
83 84 85 86 87 88 89 90 91 92 93
import React, { useState } from "react";
import { Uploader, Button } from '@nutui/nutui-react';

const App = () => {
  const uploadUrl = 'https://my-json-server.typicode.com/linrufeng/demo/posts'
  const onDelete = (file: FileItem, fileList: FileItem[]) => {
    console.log('delete 事件触发', file, fileList)
  }
  return (
    <>
      <h2>上传状态</h2>
O
oasis-cloud 已提交
94
      <Uploader url={uploadUrl} multiple removeImage={onDelete} />
95 96 97 98
    </>
  )
}
export default App;
L
liuyijun 已提交
99
```
J
junjun666 已提交
100
:::
101 102 103


### 限制上传数量5个
L
liuyijun 已提交
104

J
junjun666 已提交
105
:::demo
L
liuyijun 已提交
106
``` tsx
107 108 109 110 111 112 113 114
import React, { useState } from "react";
import { Uploader, Button } from '@nutui/nutui-react';

const App = () => {
  const uploadUrl = 'https://my-json-server.typicode.com/linrufeng/demo/posts'
  return (
    <>
      <h2>限制上传数量5个</h2>
O
oasis-cloud 已提交
115
      <Uploader url={uploadUrl} multiple maximum="5" />
116 117 118 119
    </>
  )
}
export default App;
L
liuyijun 已提交
120
```
J
junjun666 已提交
121 122
:::

123
### 限制上传大小(每个文件最大不超过 50kb,也可以在beforeupload中自行处理)
L
liuyijun 已提交
124

J
junjun666 已提交
125
:::demo
126 127 128 129 130 131 132 133 134 135 136 137
``` tsx
import React, { useState } from "react";
import { Uploader, Button } from '@nutui/nutui-react';

const App = () => {
  const uploadUrl = 'https://my-json-server.typicode.com/linrufeng/demo/posts'
  const onOversize = (files: File[]) => {
    console.log('oversize 触发 文件大小不能超过 50kb', files)
  }
  return (
    <>
      <h2>限制上传大小(每个文件最大不超过 50kb)</h2>
O
oasis-cloud 已提交
138
      <Uploader url={uploadUrl} multiple maximize={1024 * 50} oversize={onOversize} />
139 140
    </>
  )
L
liuyijun 已提交
141
}
142
export default App;
L
liuyijun 已提交
143
```
J
junjun666 已提交
144
:::
L
liuyijun 已提交
145

146

L
liuyijun 已提交
147 148
### 自定义 FormData headers

J
junjun666 已提交
149
:::demo
L
liuyijun 已提交
150
``` tsx
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
import React, { useState } from "react";
import { Uploader, Button } from '@nutui/nutui-react';

const App = () => {
  const uploadUrl = 'https://my-json-server.typicode.com/linrufeng/demo/posts'
  const formData = {
    custom: 'test',
  }
  return (
    <>
      <h2>自定义 FormData headers</h2>
      <Uploader
        url={uploadUrl}
        data={formData}
        headers={formData}
O
oasis-cloud 已提交
166 167
        withCredentials
       />
168 169 170 171
    </>
  )
}
export default App;
L
liuyijun 已提交
172
```
J
junjun666 已提交
173
:::
L
liuyijun 已提交
174

175 176
### 手动上传

J
junjun666 已提交
177
:::demo
178 179 180 181 182 183 184 185 186 187 188 189 190
``` tsx
import React, { useState, useRef } from "react";
import { Uploader, Button } from '@nutui/nutui-react';

const App = () => {
  const uploadUrl = 'https://my-json-server.typicode.com/linrufeng/demo/posts'
  const uploadRef = useRef(null)
  const submitUpload = () => {
    uploadRef.current.submit()
  }
  return (
    <>
      <h2>手动上传</h2>
O
oasis-cloud 已提交
191
      <Uploader url={uploadUrl} maximum="5" autoUpload={false} ref={uploadRef} />
192 193 194 195 196 197
      <br />
      <Button type="success" size="small" onClick={submitUpload}>
        执行上传
      </Button>
    </>
  )
L
liuyijun 已提交
198
}
199
export default App;
L
liuyijun 已提交
200
```
J
junjun666 已提交
201
:::
L
liuyijun 已提交
202 203 204

### 禁用状态

J
junjun666 已提交
205
:::demo
L
liuyijun 已提交
206
``` tsx
207 208 209 210 211 212 213
import React, { useState } from "react";
import { Uploader, Button } from '@nutui/nutui-react';

const App = () => {
  return (
    <>
      <h2>禁用状态</h2>
O
oasis-cloud 已提交
214
      <Uploader disabled />
215 216 217 218
    </>
  )
}
export default App;
L
liuyijun 已提交
219
```
J
junjun666 已提交
220
:::
L
liuyijun 已提交
221 222 223 224 225 226 227 228

### Prop

| 字段              | 说明                                                                                                                                                                                   | 类型                              | 默认值           |
|-------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------|------------------|
| name              | `input` 标签 `name` 的名称,发到后台的文件参数名                                                                                                                                       | String                            | "file"           |
| url               | 上传服务器的接口地址                                                                                                                                                                   | String                            | -                |
| isPreview        | 是否上传成功后展示预览图                                                                                                                                                               | Boolean                           | true             |
229
| defaultImg        | 当上传非图片('image')格式的默认图片地址                                                                                                                                                               | String                           | ''             |
L
liuyijun 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
| isDeletable      | 是否展示删除按钮                                                                                                                                                                       | Boolean                           | true             |
| method            | 上传请求的 http method                                                                                                                                                                 | String                            | "post"           |
| capture           | 图片[选取模式](https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/input#htmlattrdefcapture),直接调起摄像头                                                                     | String                            | false            |
| maximize          | 可以设定最大上传文件的大小(字节)                                                                                                                                                     | Number丨String                    | Number.MAX_VALUE |
| maximum           | 文件上传数量限制                                                                                                                                                                       | Number丨String                    | 1                |
| clearInput       | 是否需要清空`input`内容,设为`true`支持重复选择上传同一个文件                                                                                                                          | Boolean                           | false            |
| accept            | 允许上传的文件类型,[详细说明](https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/Input/file#%E9%99%90%E5%88%B6%E5%85%81%E8%AE%B8%E7%9A%84%E6%96%87%E4%BB%B6%E7%B1%BB%E5%9E%8B) | String                            | *                |
| headers           | 设置上传的请求头部                                                                                                                                                                     | Object                            | {}               |
| data              | 附加上传的信息 formData                                                                                                                                                                | Object                            | {}               |
| uploadIcon       | 上传区域[图标名称](#/zh-CN/icon)或图片链接                                                                                                                                             | String                            | "photograph"     |
| xhrState         | 接口响应的成功状态(status)值                                                                                                                                                         | Number                            | 200              |
| withCredentials  | 支持发送 cookie 凭证信息                                                                                                                                                               | Boolean                           | fasle            |
| multiple          | 是否支持文件多选                                                                                                                                                                       | Boolean                           | fasle            |
| disabled          | 是否禁用文件上传                                                                                                                                                                       | Boolean                           | fasle            |
| timeout           | 超时时间,单位为毫秒                                                                                                   | Number丨String                    | 1000 * 30                 |
| beforeUpload     | 上传前的函数需要返回一个`Promise`对象                                                                                                                                                  | Function                          | null             |
| beforeDelete     | 除文件时的回调,返回值为 false 时不移除。支持返回一个 `Promise` 对象,`Promise` 对象 resolve(false) 或 reject 时不移除                                                                 | Function(file): boolean 丨Promise | -                |



### FileItem

| 名称     | 说明                                                    | 默认值                          |
|----------|---------------------------------------------------------|---------------------------------|
| status   | 文件状态值,可选'ready,uploading,success,error,removed' | "ready"                         |
| uid      | 文件的唯一标识                                          | new Date().getTime().toString() |
| name     | 文件名称                                                | ""                              |
| url      | 文件路径                                                | ""                              |
| type     | 文件类型                                                | "image/jpeg"                    |
| formData | 上传所需的data                                          | new FormData()                  |

### Event

| 名称     | 说明                   | 回调参数             |
|----------|------------------------|----------------------|
| start    | 文件上传开始           | options              |
| progress | 文件上传的进度         | event,options        |
| oversize | 文件大小超过限制时触发 | files                |
| success  | 上传成功               | responseText,options |
| failure  | 上传失败               | responseText,options |
| change   | 上传文件改变时的状态   | fileList,event       |
| removeImage   | 文件删除之前的状态     | files,fileList       |