-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.tsx
55 lines (47 loc) · 1.52 KB
/
upload.tsx
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
import React, { Component } from 'react';
export default function Upload() {
const [ file, setfile ] = React.useState<File>();
const handlefile = (event: React.ChangeEvent<HTMLInputElement>) => {
console.log(event.target.files);
let currentfile = event.target.files;
if (currentfile!=null) { setfile(currentfile[0] )
console.log(currentfile[0]);
}
}
const uploadFile = function (e: React.MouseEvent<HTMLSpanElement, MouseEvent>) {
e.preventDefault()
console.log(file)
if (file) {
console.log(file);
const formData = new FormData();
formData.append("file", file);
formData.append("name", file.name);
console.log(file.name);
fetch(
'/upload',
{
method: 'POST',
body: formData,
}
)
.then((response) => response.json())
.then((result) => {
console.log('Success:', result);
})
.catch((error) => {
console.error('Error:', error);
});
};
};
return(
<div>
<form>
<div>
<label> Select File</label>
<input type='file' name='file' onChange={handlefile}/>
</div>
<button onClick={uploadFile}>Upload</button>
</form>
</div>
)
}