Skip to content

Commit 5b6537d

Browse files
authored
Add possibility to pass in additional FormData fields via data prop (#8)
1 parent d0ce935 commit 5b6537d

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ abortUpload('file://some-local-file.jpg');
105105
<td>Optional</td>
106106
<td>The HTTP method for the request. Defaults to "POST".</td>
107107
</tr>
108+
<tr>
109+
<td>data</td>
110+
<td>object</td>
111+
<td>Optional</td>
112+
<td>An object of additional FormData fields to be set with the request.</td>
113+
</tr>
108114
<tr>
109115
<td>headers</td>
110116
<td>Headers</td>
@@ -163,7 +169,7 @@ useFileUpload({ headers });
163169
```
164170

165171
</td>
166-
</tr>
172+
</tr>
167173
<tr>
168174
<td>onError</td>
169175
<td>function</td>
@@ -196,7 +202,7 @@ useFileUpload({ headers });
196202
```
197203

198204
</td>
199-
</tr>
205+
</tr>
200206
</tbody>
201207
</table>
202208

src/hooks/useFileUpload.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default function useFileUpload<T extends UploadItem = UploadItem>({
1212
method = 'POST',
1313
headers,
1414
timeout,
15+
data,
1516
onProgress,
1617
onDone,
1718
onError,
@@ -26,6 +27,10 @@ export default function useFileUpload<T extends UploadItem = UploadItem>({
2627
const formData = new FormData();
2728
formData.append(field, item);
2829

30+
if (data)
31+
for (const key in data)
32+
formData.append(key, data[key]);
33+
2934
const xhr = new XMLHttpRequest();
3035

3136
xhr.open(method, url);

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export type FileUploadOptions<T extends UploadItem = UploadItem> = {
3333
method?: string;
3434
headers?: Headers;
3535
timeout?: number;
36+
data?: {[key: string]: string};
3637
onProgress?: (data: OnProgressData<T>) => void;
3738
onDone?: (data: OnDoneData<T>) => void;
3839
onError?: (data: OnErrorData<T>) => void;

0 commit comments

Comments
 (0)