doc.md 11.7 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 15 16
```

## 代码示例

### 基本用法

17
:::demo
L
liuyijun 已提交
18
``` tsx
19 20 21 22 23 24 25 26 27 28 29
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 已提交
30
      <Uploader url={uploadUrl} start={onStart} />
31 32 33 34
    </>
  )
}
export default App;
L
liuyijun 已提交
35
```
J
junjun666 已提交
36
:::
L
liuyijun 已提交
37 38 39

### 自定义上传样式

J
junjun666 已提交
40
:::demo
L
liuyijun 已提交
41
``` tsx
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
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 已提交
59
```
J
junjun666 已提交
60
:::
L
liuyijun 已提交
61 62

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

:::demo
L
liuyijun 已提交
65
``` tsx
66 67 68 69 70 71 72 73
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 已提交
74
      <Uploader capture url={uploadUrl} />
75 76 77 78
    </>
  )
}
export default App;
L
liuyijun 已提交
79
```
J
junjun666 已提交
80
:::
L
liuyijun 已提交
81

82
### 上传状态
J
junjun666 已提交
83 84

:::demo
L
liuyijun 已提交
85
``` tsx
86 87 88 89 90 91 92 93 94 95 96
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 已提交
97
      <Uploader url={uploadUrl} multiple removeImage={onDelete} />
98 99 100 101
    </>
  )
}
export default App;
L
liuyijun 已提交
102
```
J
junjun666 已提交
103
:::
104 105 106


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

J
junjun666 已提交
108
:::demo
L
liuyijun 已提交
109
``` tsx
110 111 112 113 114 115 116 117
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 已提交
118
      <Uploader url={uploadUrl} multiple maximum="5" />
119 120 121 122
    </>
  )
}
export default App;
L
liuyijun 已提交
123
```
J
junjun666 已提交
124 125
:::

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

J
junjun666 已提交
128
:::demo
129 130 131 132 133 134 135 136 137 138 139 140
``` 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 已提交
141
      <Uploader url={uploadUrl} multiple maximize={1024 * 50} oversize={onOversize} />
142 143
    </>
  )
L
liuyijun 已提交
144
}
145
export default App;
L
liuyijun 已提交
146
```
J
junjun666 已提交
147
:::
L
liuyijun 已提交
148

149

L
liuyijun 已提交
150 151
### 自定义 FormData headers

J
junjun666 已提交
152
:::demo
L
liuyijun 已提交
153
``` tsx
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
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 已提交
169 170
        withCredentials
       />
171 172 173 174
    </>
  )
}
export default App;
L
liuyijun 已提交
175
```
J
junjun666 已提交
176
:::
L
liuyijun 已提交
177

178 179
### 手动上传

J
junjun666 已提交
180
:::demo
181 182 183 184 185 186 187 188 189 190 191 192 193
``` 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 已提交
194
      <Uploader url={uploadUrl} maximum="5" autoUpload={false} ref={uploadRef} />
195 196 197 198 199 200
      <br />
      <Button type="success" size="small" onClick={submitUpload}>
        执行上传
      </Button>
    </>
  )
L
liuyijun 已提交
201
}
202
export default App;
L
liuyijun 已提交
203
```
J
junjun666 已提交
204
:::
L
liuyijun 已提交
205 206 207

### 禁用状态

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

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

### Prop

| 字段              | 说明                                                                                                                                                                                   | 类型                              | 默认值           |
|-------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------|------------------|
| name              | `input` 标签 `name` 的名称,发到后台的文件参数名                                                                                                                                       | String                            | "file"           |
| url               | 上传服务器的接口地址                                                                                                                                                                   | String                            | -                |
| isPreview        | 是否上传成功后展示预览图                                                                                                                                                               | Boolean                           | true             |
| 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       |