-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.md.orig
82 lines (62 loc) · 3.02 KB
/
README.md.orig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/3c3d99235106488c8fc1294baa609fa8)](https://app.codacy.com/gh/pantoninho/use-uploader/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
[![Codacy Badge](https://app.codacy.com/project/badge/Coverage/3c3d99235106488c8fc1294baa609fa8)](https://app.codacy.com/gh/pantoninho/use-uploader/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_coverage)
# use-uploader
a react hook for uploading files with support for concurrent multipart uploads and progress tracking.
## installation
```sh
npm install @pantoninho/use-uploader
```
## API
### `useUploader(options)`
a hook for uploading files with support for concurrent multipart uploads and progress tracking.
#### arguments
1. `options` (`Object`): an object containing the following properties:
- `threads` (`number`, optional): the number of concurrent uploads **per file**. defaults to `5`.
#### returns
an object with the following properties:
- `upload` (`Function`): a function that uploads a file. It receives an object or an array of objects with the following properties:
- `file` (`File`): the file to upload.
- `to` (`string`): URL to upload the file to.
- `uploads` (`object`): a object containing the upload progress for each file. the keys are the filenames and their destinations, and the values are objects with the following properties:
- `key` (`string`): a unique key for the upload.
- `isUploading` (`boolean`): a boolean indicating whether the file is being uploaded.
- `progress` (`number`): the file upload progress as a number between `0` and `1`.
- `loaded` (`number`): the number of bytes uploaded.
- `total` (`number`): the total file size in bytes.
- `data` (`any`): data returned by the response
- `error` (`Error`): error returned by the response
## example
```jsx
import React from 'react';
import { useUploader } from '@pantoninho/use-uploader';
function App() {
const { upload, uploads } = useUploader();
const handleUpload = async (file) => {
const requests = [{ file, to: 'https://upload.example/1' }];
await upload(requests);
};
return (
<div>
<input
type="file"
onChange={(e) => handleUpload(e.target.files[0])}
/>
{Object.keys(state.uploads).map((filename) =>
Object.keys(state.uploads[filename]).map((destination) => {
const upload = state.uploads[filename][destination];
return (
<p key={upload.key}>
{upload.isUploading
? `${filename}: ${upload.progress}`
: `${filename}: upload complete`}
{upload.error &&
`${filename}: ${upload.error.message}`}
</p>
);
}),
)}
</div>
);
}
export default App;
```