doc.md 17.6 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
### 上传状态

:::demo
``` tsx
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 defaultFileList: FileType<string>[] = [
    {
      name: '文件1.png',
      url: 'https://m.360buyimg.com/babel/jfs/t1/164410/22/25162/93384/616eac6cE6c711350/0cac53c1b82e1b05.gif',
      status: 'success',
      message: '上传成功',
      type: 'image',
      uid: '123',
    },
    {
      name: '文件2.png',
      url: 'https://m.360buyimg.com/babel/jfs/t1/164410/22/25162/93384/616eac6cE6c711350/0cac53c1b82e1b05.gif',
      status: 'error',
      message: '上传失败',
      type: 'image',
      uid: '124',
    },
    {
      name: '文件3.png',
      url: 'https://m.360buyimg.com/babel/jfs/t1/164410/22/25162/93384/616eac6cE6c711350/0cac53c1b82e1b05.gif',
      status: 'uploading',
      message: '上传中...',
      type: 'image',
      uid: '125',
    },
  ]
  const onDelete = (file: FileItem, fileList: FileItem[]) => {
    console.log(translated.ca3903f3, file, fileList)
  }
  return (
    <>
      <h2>上传状态</h2>
      <Uploader
        url={uploadUrl}
        defaultFileList={defaultFileList}
        onRemove={onDelete}
        maximum="3"
        multiple
        uploadIcon="dongdong"
      />
    </>
  )
}
export default App;
```
:::

L
liuyijun 已提交
91 92
### 自定义上传样式

J
junjun666 已提交
93
:::demo
L
liuyijun 已提交
94
``` tsx
95 96 97 98 99 100 101 102 103
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}>
104
        <Button type="success" size="small">
105 106 107 108 109 110 111
          上传文件
        </Button>
      </Uploader>
    </>
  )
}
export default App;
L
liuyijun 已提交
112
```
J
junjun666 已提交
113
:::
L
liuyijun 已提交
114

115
### 自定义上传使用默认进度条
J
junjun666 已提交
116 117

:::demo
L
liuyijun 已提交
118
``` tsx
119
import React, { useState } from "react";
120
import { Uploader, Button, Progress } from '@nutui/nutui-react';
121 122 123

const App = () => {
  const uploadUrl = 'https://my-json-server.typicode.com/linrufeng/demo/posts'
124 125 126 127
  const [progressPercent, setProgressPercent] = useState(0)
  const onProgress = ({ event, options, percentage }: any) => {
    setProgressPercent(percentage)
  }
128 129
  return (
    <>
130 131 132 133 134 135 136 137 138 139 140 141
      <h2>自定义上传使用默认进度条</h2>
      <Uploader url={uploadUrl} onProgress={onProgress}>
        <Button type="success" size="small">
          上传文件
        </Button>
      </Uploader>
      <br />
      <Progress
        percentage={progressPercent}
        strokeColor="linear-gradient(270deg, rgba(18,126,255,1) 0%,rgba(32,147,255,1) 32.815625%,rgba(13,242,204,1) 100%)"
        status
      />
142 143 144 145
    </>
  )
}
export default App;
L
liuyijun 已提交
146
```
J
junjun666 已提交
147
:::
L
liuyijun 已提交
148

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

:::demo
L
liuyijun 已提交
152
``` tsx
153 154 155 156 157 158 159
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 (
    <>
160 161
      <h2>直接调起摄像头(移动端生效)</h2>
      <Uploader capture url={uploadUrl} />
162 163 164 165
    </>
  )
}
export default App;
L
liuyijun 已提交
166
```
J
junjun666 已提交
167
:::
168 169

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

J
junjun666 已提交
171
:::demo
L
liuyijun 已提交
172
``` tsx
173 174 175 176 177 178 179 180
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 已提交
181
      <Uploader url={uploadUrl} multiple maximum="5" />
182 183 184 185
    </>
  )
}
export default App;
L
liuyijun 已提交
186
```
J
junjun666 已提交
187 188
:::

189
### 限制上传大小(每个文件最大不超过 50kb)
L
liuyijun 已提交
190

J
junjun666 已提交
191
:::demo
192 193 194 195 196 197 198 199 200 201 202 203
``` 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 已提交
204
      <Uploader url={uploadUrl} multiple maximize={1024 * 50} oversize={onOversize} />
205 206
    </>
  )
L
liuyijun 已提交
207
}
208
export default App;
L
liuyijun 已提交
209
```
J
junjun666 已提交
210
:::
L
liuyijun 已提交
211

212

L
liuyijun 已提交
213 214
### 自定义 FormData headers

J
junjun666 已提交
215
:::demo
L
liuyijun 已提交
216
``` tsx
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
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 已提交
232 233
        withCredentials
       />
234 235 236 237
    </>
  )
}
export default App;
L
liuyijun 已提交
238
```
J
junjun666 已提交
239
:::
L
liuyijun 已提交
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
### 自定义 xhr 上传方式(before-xhr-upload)

:::demo
``` 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 beforeXhrUpload = (xhr: XMLHttpRequest, options: any) => {
    if (options.method.toLowerCase() == 'put') {
      xhr.send(options.sourceFile);
    } else {
      xhr.send(options.formData);
    }
  };
  return (
    <>
      <h2>自定义 xhr 上传方式(before-xhr-upload)</h2>
      <Uploader
        url={uploadUrl}
        method="put"
        onBeforeXhrUpload={beforeXhrUpload}
       />
    </>
  )
}
export default App;
```
:::

272 273
### 手动上传

J
junjun666 已提交
274
:::demo
275 276 277 278 279 280 281 282 283 284 285 286 287
``` 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 已提交
288
      <Uploader url={uploadUrl} maximum="5" autoUpload={false} ref={uploadRef} />
289 290 291 292 293 294
      <br />
      <Button type="success" size="small" onClick={submitUpload}>
        执行上传
      </Button>
    </>
  )
L
liuyijun 已提交
295
}
296
export default App;
L
liuyijun 已提交
297
```
J
junjun666 已提交
298
:::
L
liuyijun 已提交
299 300 301

### 禁用状态

J
junjun666 已提交
302
:::demo
L
liuyijun 已提交
303
``` tsx
304 305 306 307 308 309 310
import React, { useState } from "react";
import { Uploader, Button } from '@nutui/nutui-react';

const App = () => {
  return (
    <>
      <h2>禁用状态</h2>
O
oasis-cloud 已提交
311
      <Uploader disabled />
312 313 314 315
    </>
  )
}
export default App;
L
liuyijun 已提交
316
```
J
junjun666 已提交
317
:::
L
liuyijun 已提交
318 319 320 321 322

### Prop

| 字段              | 说明                                                                                                                                                                                   | 类型                              | 默认值           |
|-------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------|------------------|
323
| autoUpload `v1.3.4`              | 是否在选取文件后立即进行上传,false 时需要手动执行 ref submit 方法进行上传                                                                                                                                       | Boolean                            | true           |
L
liuyijun 已提交
324 325
| name              | `input` 标签 `name` 的名称,发到后台的文件参数名                                                                                                                                       | String                            | "file"           |
| url               | 上传服务器的接口地址                                                                                                                                                                   | String                            | -                |
326
| defaultFileList               | 默认已经上传的文件列表                                                                                                                                                                   | FileItem[]                            | []                |
L
liuyijun 已提交
327
| isPreview        | 是否上传成功后展示预览图                                                                                                                                                               | Boolean                           | true             |
328
| defaultImg        | 当上传非图片('image')格式的默认图片地址                                                                                                                                                               | String                           | ''             |
L
liuyijun 已提交
329 330
| isDeletable      | 是否展示删除按钮                                                                                                                                                                       | Boolean                           | true             |
| method            | 上传请求的 http method                                                                                                                                                                 | String                            | "post"           |
331
| listType `v1.3.4`            | 上传列表的内建样式,支持两种基本样式 picture、list                                                                                                                                                                 | String                            | "picture"           |
L
liuyijun 已提交
332 333 334
| 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                |
335
| clearInput       | 是否需要清空`input`内容,设为`true`支持重复选择上传同一个文件                                                                                                                          | Boolean                           | true            |
L
liuyijun 已提交
336 337 338 339
| 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"     |
340
| uploadIconSize `v1.3.4`       | 上传区域[图标尺寸](#/icon)大小,如 `20px` `2em` `2rem`                                                                                                                                             | String or Number                            | -     |
L
liuyijun 已提交
341
| xhrState         | 接口响应的成功状态(status)值                                                                                                                                                         | Number                            | 200              |
342 343 344
| withCredentials  | 支持发送 cookie 凭证信息                                                                                                                                                               | Boolean                           | false            |
| multiple          | 是否支持文件多选                                                                                                                                                                       | Boolean                           | false            |
| disabled          | 是否禁用文件上传                                                                                                                                                                       | Boolean                           | false            |
L
liuyijun 已提交
345
| timeout           | 超时时间,单位为毫秒                                                                                                   | Number丨String                    | 1000 * 30                 |
346
| beforeUpload `v1.3.4 废弃`     | 上传前的函数需要返回一个`Promise`对象                                                                                                                                                  | Function                          | null             |
347 348
| onBeforeUpload `v1.3.4`     | 上传前的函数需要返回一个`Promise`对象                                                                                                                                                  | Function                          | null             |
| onBeforeXhrUpload `v1.3.4`     | 执行 XHR 上传时,自定义方式                                                                                                                                                  | Function(xhr,option)                          | null             |
349
| beforeDelete  `v1.3.4 废弃`   | 除文件时的回调,返回值为 false 时不移除。支持返回一个 `Promise` 对象,`Promise` 对象 resolve(false) 或 reject 时不移除                                                                 | Function(file): boolean 丨Promise | -                |
350
| onBeforeDelete  `v1.3.4`  | 除文件时的回调,返回值为 false 时不移除。支持返回一个 `Promise` 对象,`Promise` 对象 resolve(false) 或 reject 时不移除                                                                 | Function(file): boolean 丨Promise | -                |
L
liuyijun 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368



### FileItem

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

### Event

| 名称     | 说明                   | 回调参数             |
|----------|------------------------|----------------------|
369
| start `v1.3.4 `    | 文件上传开始           | options              |
370
| onStart `v1.3.4`    | 文件上传开始           | options              |
371
| progress `v1.3.4 废弃` | 文件上传的进度         | event,options        |
372
| onProgress `v1.3.4` | 文件上传的进度         | event,options,percentage        |
373
| oversize `v1.3.4 废弃` | 文件大小超过限制时触发 | files                |
374
| onOversize `v1.3.4` | 文件大小超过限制时触发 | files                |
375
| success `v1.3.4 废弃`  | 上传成功               | responseText,options |
376
| onSuccess `v1.3.4`  | 上传成功               | responseText,options |
377
| failure `v1.3.4 废弃`  | 上传失败               | responseText,options |
378 379
| onFailure `v1.3.4`  | 上传失败               | responseText,options |
| onChange `v1.3.4`   | 上传文件改变时的状态   | fileList,event       |
380
| change `v1.3.4 废弃`  | 上传文件改变时的状态   | fileList,event       |
381
| onRemove `v1.3.4`   | 文件删除之前的状态     | files,fileList       |
382
| removeImage  `v1.3.4 废弃` | 文件删除之前的状态     | files,fileList       |
383
| onFileItemClick `v1.3.4`   | 文件上传成功后点击触发     | fileItem       |
L
liuyijun 已提交
384

385 386 387 388 389 390 391 392 393 394 395 396 397


## 主题定制

### 样式变量

组件提供了下列 CSS 变量,可用于自定义样式,使用方法请参考 [ConfigProvider 组件](#/zh-CN/component/configprovider)

| 名称 | 默认值 |
| --- | --- |
| --nutui-uploader-picture-width | ` 100px` |
| --nutui-uploader-picture-height | ` 100px` |
| --nutui-uploader-background | ` $gray4` |