@@ -8,41 +8,37 @@ const Uploader = (props) => {
8
8
console . log ( "file upload" , event )
9
9
}
10
10
11
+ const onProgress = ( event ) => {
12
+ console . log ( "progress" , event )
13
+ }
14
+
11
15
const uploadHandler = function ( { files } ) {
12
- const [ file ] = files ;
13
- let formData = new FormData ( ) ;
14
- formData . append ( 'demoFile' , file ) ;
15
16
16
- fetch ( serverHost + "/match/upload" ,
17
- {
18
- method : 'POST' ,
19
- body : formData
20
- } ,
21
- ) . catch ( err => {
22
- console . log ( "failed to upload" )
23
- } )
17
+ const uploadChunk = function ( chunk , filename ) {
18
+ console . log ( "uploading chunk" , filename )
24
19
25
- // const fileReader = new FileReader();
26
- // fileReader.onload = (e) => {
27
- // uploadDemo(e.target.result);
28
- // };
29
- // fileReader.readAsDataURL(file);
30
- }
20
+ let formData = new FormData ( ) ;
21
+ formData . append ( "demoFile" , chunk , filename ) ;
31
22
32
- const uploadDemo = async ( demoFile ) => {
33
- let formData = new FormData ( ) ;
34
- formData . append ( 'demoFile' , demoFile ) ;
23
+ let xhr = new XMLHttpRequest ( ) ;
24
+ xhr . upload . addEventListener ( 'progress' , onProgress ) ;
25
+ xhr . onreadystatechange = ( xhr , event ) => {
26
+ console . log ( "onreadystatechange" , xhr , event )
27
+ } ;
28
+ xhr . open ( 'POST' , serverHost + "/match/upload" , true ) ;
29
+ xhr . withCredentials = props . withCredentials ;
30
+ xhr . send ( formData ) ;
31
+ }
35
32
36
- const response = await fetch ( serverHost + "/match/upload" ,
37
- {
38
- method : 'POST' ,
39
- body : formData
40
- } ,
41
- ) ;
42
- console . log ( response )
43
- } ;
44
- const onProgress = ( event ) => {
45
- console . log ( "progress" , event )
33
+ const [ file ] = files ;
34
+ const chunkSize = 1024 * 1024 * 30 ; // 30MB
35
+
36
+ let start = 0 ;
37
+ let chunkNo = 0 ;
38
+ while ( start < file . size ) {
39
+ uploadChunk ( file . slice ( start , start + chunkSize ) , file . name + "_" + chunkNo ++ ) ;
40
+ start += chunkSize ;
41
+ }
46
42
}
47
43
48
44
return (
@@ -55,8 +51,8 @@ const Uploader = (props) => {
55
51
maxFileSize = { 200_000_000 }
56
52
onUpload = { onUpload }
57
53
onProgress = { onProgress }
58
- // customUpload={true}
59
- // uploadHandler={uploadHandler}
54
+ customUpload = { true }
55
+ uploadHandler = { uploadHandler }
60
56
auto />
61
57
</ div >
62
58
)
0 commit comments