Skip to content

Commit 961cc35

Browse files
codeserkwvds
authored andcommitted
* Changes in FileUload to use chunk upload in some cases
* `chunk-enabled` prop added to enable chunk upload * `chunk` prop added to modify chunk upload parameters * `ChunkUploadHandler` class created to handler chunk upload process * Example added to the docs * Chunk documentation added to the docs
1 parent 7d88edc commit 961cc35

File tree

13 files changed

+13050
-37
lines changed

13 files changed

+13050
-37
lines changed

.eslintrc.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// http://eslint.org/docs/user-guide/configuring
2+
3+
module.exports = {
4+
root: true,
5+
parser: 'babel-eslint',
6+
parserOptions: {
7+
sourceType: 'module'
8+
},
9+
env: {
10+
browser: true,
11+
jest: true
12+
},
13+
// https://github.com/standard/standard/blob/master/docs/RULES-en.md
14+
extends: "standard",
15+
16+
// required to lint *.vue files
17+
plugins: [
18+
'html'
19+
],
20+
// add your custom rules here
21+
'rules': {
22+
// allow paren-less arrow functions
23+
'arrow-parens': 0,
24+
// allow async-await
25+
'generator-star-spacing': 0,
26+
// allow debugger during development
27+
'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0,
28+
29+
'no-useless-escape': 0,
30+
'comma-dangle': 0,
31+
'space-before-function-paren': 0,
32+
'no-multiple-empty-lines': 0,
33+
'no-multi-spaces': 0,
34+
'padded-blocks': 0,
35+
'prefer-promise-reject-errors': 0,
36+
'operator-linebreak': 0
37+
}
38+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- [x] `PUT` method
1616
- [x] Customize the filter
1717
- [x] thumbnails
18+
- [x] Chunk upload
1819

1920

2021

docs/docs/en.md

Lines changed: 140 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,81 @@ new Vue({
131131
</html>
132132
```
133133

134+
### Chunk Upload
134135

136+
This package allows chunk uploads, which means you can upload a file in different parts.
137+
138+
This process is divided in three phases: <strong>start</strong>, <strong>upload</strong>,<strong>finish</strong></p>
139+
140+
#### start
141+
142+
This is the first phase of the process. We'll tell the backend that we are going to upload a file, with certain `size` and `mime_type`.
143+
144+
Use the option `startBody` to add more parameters to the body of this request.
145+
146+
The backend will provide a `session_id` (to identify the upload) and a `end_offset` which is the size of every chunk
147+
148+
#### upload
149+
150+
In this phase we'll upload every chunk until all of them are uploaded. This step allows some failures in the backend, and will retry up to `maxRetries` times.
151+
152+
We'll send the `session_id`, `start_chunk` and `chunk` (the sliced blob - part of file we are uploading). We expect the backend to return `status = 'success'`, we'll retry otherwise.
153+
154+
Use the option `uploadBody` to add more parameters to the body of this request.
155+
156+
#### finish
157+
158+
In this phase we tell the backend that there are no more chunks to upload, so it can wrap everything. We send the `session_id` in this phase.
159+
160+
Use the option `finishBody` to add more parameters to the body of this request.
161+
162+
#### Example
163+
164+
```html
165+
<file-upload
166+
ref="upload"
167+
v-model="files"
168+
post-action="/post.method"
169+
put-action="/put.method"
170+
171+
chunk-enabled
172+
:chunk="{
173+
action: '/upload/chunk',
174+
minSize: 1048576,
175+
maxActive: 3,
176+
maxRetries: 5,
177+
178+
// In this case our backend also needs the user id to operate
179+
startBody: {
180+
user_id: user.id
181+
}
182+
}"
183+
184+
@input-file="inputFile"
185+
@input-filter="inputFilter"
186+
>
187+
Upload file
188+
</file-upload>
189+
```
190+
191+
#### Extending the handler
192+
193+
We are using the class `src/chunk/ChunkUploadHandler` class to implement this protocol. You can extend this class (or even create a different one from scratch) to implement your own way to communicat with the backend.
194+
195+
This class must implement a method called `upload` which **must** return a promise. This promise will be used by the `FileUpload` component to determinate whether the file was uploaded or failed.
196+
197+
Use the `handler` parameter to use a different Handler
198+
199+
```html
200+
:chunk="{
201+
action: '/upload/chunk',
202+
minSize: 1048576,
203+
maxActive: 3,
204+
maxRetries: 5,
205+
206+
handler: MyHandlerClass
207+
}
208+
```
135209

136210
### SSR (Server isomorphism)
137211

@@ -213,9 +287,9 @@ const nodeExternals = require('webpack-node-externals');
213287
}
214288
```
215289

216-
* [https://github.com/liady/webpack-node-externals](https://github.com/liady/webpack-node-externals)
290+
* [https://github.com/liady/webpack-node-externals](https://github.com/liady/webpack-node-externals)
217291

218-
* [**`vue-hackernews` demo**](https://github.com/lian-yue/vue-hackernews-2.0/)
292+
* [**`vue-hackernews` demo**](https://github.com/lian-yue/vue-hackernews-2.0/)
219293

220294
* [**View changes**](https://github.com/lian-yue/vue-hackernews-2.0/commit/bd6c58a30cc6b8ba6c0148e737b3ce9336b99cf8)
221295

@@ -290,9 +364,9 @@ The `name` attribute of the input tag
290364

291365
* **Browser:** `> IE9`
292366

293-
* **Details:**
367+
* **Details:**
294368

295-
`put-action` is not empty Please give priority to` PUT` request
369+
`put-action` is not empty Please give priority to` PUT` request
296370

297371
* **Usage:**
298372
```html
@@ -324,7 +398,7 @@ Attach `header` data
324398

325399
### data
326400

327-
`POST request`: Append request `body`
401+
`POST request`: Append request `body`
328402
`PUT request`: Append request `query`
329403

330404
* **Type:** `Object`
@@ -347,9 +421,9 @@ File List
347421

348422
* **Default:** `[]`
349423

350-
* **Details:**
424+
* **Details:**
351425

352-
View **[`File`](#file)** details
426+
View **[`File`](#file)** details
353427
> In order to prevent unpredictable errors, can not directly modify the `files`, please use [`add`](#instance-methods-add), [`update`](#instance-methods-update), [`remove`](#instance-methods-remove) method to modify
354428
355429
* **Usage:**
@@ -365,7 +439,7 @@ File List
365439

366440
### accept
367441

368-
The `accept` attribute of the input tag, MIME type
442+
The `accept` attribute of the input tag, MIME type
369443

370444
* **Type:** `String`
371445

@@ -386,14 +460,14 @@ The `accept` attribute of the input tag, MIME type
386460

387461
### multiple
388462

389-
The `multiple` attribute of the input tag
390-
Whether to allow multiple files to be selected
463+
The `multiple` attribute of the input tag
464+
Whether to allow multiple files to be selected
391465

392466
* **Type:** `Boolean`
393467

394468
* **Default:** `false`
395469

396-
* **Details:**
470+
* **Details:**
397471

398472
If it is `false` file inside only one file will be automatically deleted
399473

@@ -406,8 +480,8 @@ Whether to allow multiple files to be selected
406480

407481
### directory
408482

409-
The `directory` attribute of the input tag
410-
Whether it is a upload folder
483+
The `directory` attribute of the input tag
484+
Whether it is a upload folder
411485

412486
* **Type:** `Boolean`
413487

@@ -426,7 +500,7 @@ Whether it is a upload folder
426500

427501
### extensions
428502

429-
Allow upload file extensions
503+
Allow upload file extensions
430504

431505
* **Type:** `Array | String | RegExp`
432506

@@ -498,7 +572,7 @@ List the maximum number of files
498572

499573
### thread
500574

501-
Also upload the number of files at the same time (number of threads)
575+
Also upload the number of files at the same time (number of threads)
502576

503577
* **Type:** `Number`
504578

@@ -512,8 +586,42 @@ Also upload the number of files at the same time (number of threads)
512586
```
513587

514588

589+
### chunk-enabled
590+
591+
Whether chunk uploads is enabled or not
592+
593+
* **Type:** `Boolean`
594+
595+
* **Default:** `false`
596+
597+
* **Usage:**
598+
```html
599+
<file-upload :chunk-enabled="true"></file-upload>
600+
<file-upload chunk-enabled></file-upload>
601+
```
602+
603+
### chunk
604+
605+
All the options to handle chunk uploads
515606

607+
* **Type:** `Object`
516608

609+
* **Default:**
610+
```js
611+
{
612+
headers: {
613+
'Content-Type': 'application/json'
614+
},
615+
action: '',
616+
minSize: 1048576,
617+
maxActive: 3,
618+
maxRetries: 5,
619+
620+
// This is the default Handler implemented in this package
621+
// you can use your own handler if your protocol is different
622+
handler: ChunkUploadDefaultHandler
623+
}
624+
```
517625

518626
### drop
519627

@@ -527,7 +635,7 @@ Drag and drop upload
527635

528636
* **Details:**
529637

530-
If set to `true`, read the parent component as a container
638+
If set to `true`, read the parent component as a container
531639

532640
* **Usage:**
533641
```html
@@ -615,7 +723,7 @@ Default for `v-model` binding
615723

616724
### @input-filter
617725

618-
Add, update, remove pre-filter
726+
Add, update, remove pre-filter
619727

620728
* **Arguments:**
621729

@@ -630,14 +738,14 @@ Add, update, remove pre-filter
630738
If the `oldFile` value is `undefined` 'is added
631739
If `newFile`, `oldFile` is exist, it is updated
632740

633-
> Synchronization modify `newFile`
634-
> Asynchronous Please use `update`,` add`, `remove`,` clear` method
741+
> Synchronization modify `newFile`
742+
> Asynchronous Please use `update`,` add`, `remove`,` clear` method
635743
> Asynchronous Please set an error first to prevent being uploaded
636744
637-
> Synchronization can not use `update`,` add`, `remove`,` clear` methods
638-
> Asynchronous can not modify `newFile`
745+
> Synchronization can not use `update`,` add`, `remove`,` clear` methods
746+
> Asynchronous can not modify `newFile`
639747
640-
* **Usage:**
748+
* **Usage:**
641749
```html
642750
<template>
643751
<ul>
@@ -712,8 +820,8 @@ Add, update, remove after
712820
If `newFile`, `oldFile` is exist, it is updated
713821

714822

715-
>You can use `update`,` add`, `remove`,` clear` methods in the event
716-
>You can not modify the `newFile` object in the event
823+
>You can use `update`,` add`, `remove`,` clear` methods in the event
824+
>You can not modify the `newFile` object in the event
717825
>You can not modify the `oldFile` object in the event
718826
719827
* **Usage:**
@@ -955,7 +1063,7 @@ Add the file selected by `<input type = "file">` to the upload list
9551063

9561064
### addDataTransfer()
9571065

958-
Add files that are dragged or pasted into the upload list
1066+
Add files that are dragged or pasted into the upload list
9591067

9601068
* **Arguments:**
9611069

@@ -1092,7 +1200,7 @@ Empty the file list
10921200

10931201
* **Details:**
10941202

1095-
If the attribute does not exist, the object will not be processed internally
1203+
If the attribute does not exist, the object will not be processed internally
10961204
If the attribute does not exist, it is not `File` but `Object`
10971205

10981206

@@ -1110,7 +1218,7 @@ File ID
11101218

11111219
* **Details:**
11121220

1113-
>`id` can not be repeated
1221+
>`id` can not be repeated
11141222
>Upload can not modify `id`
11151223
11161224

@@ -1129,7 +1237,7 @@ File size
11291237

11301238
### name
11311239

1132-
Filename
1240+
Filename
11331241

11341242
* **Type:** `String`
11351243

@@ -1139,7 +1247,7 @@ Filename
11391247

11401248
* **Details:**
11411249

1142-
Format: `directory/filename.gif` `filename.gif`
1250+
Format: `directory/filename.gif` `filename.gif`
11431251

11441252

11451253

@@ -1174,8 +1282,8 @@ Activation or abort upload
11741282

11751283
* **Details:**
11761284

1177-
`true` = Upload
1178-
`false` = Abort
1285+
`true` = Upload
1286+
`false` = Abort
11791287

11801288

11811289

@@ -1194,7 +1302,7 @@ Upload failed error code
11941302

11951303
* **Details:**
11961304

1197-
Built-in
1305+
Built-in
11981306
`size`, `extension`, `timeout`, `abort`, `network`, `server`, `denied`
11991307

12001308

docs/i18n/en.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default {
2525
avatar: 'Upload avatar',
2626
drag: 'Drag and drop',
2727
multiple: 'Multiple instances',
28+
chunk: 'Chunk upload',
2829
vuex: 'Vuex',
2930
}
3031
}

0 commit comments

Comments
 (0)