I want to cancel the upload task at the click of a button. But it is not working(it returns false and I can view the file in the firebase storage console) below is the full code of the component
Upload.js
JavaScript
x
104
104
1
import React, { useState } from "react";
2
import { ref, uploadBytesResumable } from "firebase/storage";
3
import { storage } from "../../firebase";
4
import bookwall from "../../illustrations/bookwall.svg";
5
6
const Upload = () => {
7
const [loading, setLoading] = useState(false);
8
//used for cancel button
9
const [cancel, setCancel] = useState(false);
10
const formHandler = (e) => {
11
e.preventDefault();
12
const file = e.target[1].files[0];
13
uploadFile(file);
14
};
15
16
const uploadFile = (file) => {
17
if (!file) return;
18
const storageRef = ref(storage, `files/${file.name}`);
19
const uploadTask = uploadBytesResumable(storageRef, file);
20
if(cancel === true){
21
uploadTask.cancel();
22
setLoading(false);
23
setCancel(false);
24
console.log('cancelled');
25
console.log(`does upload task cancel work? ${uploadTask.cancel()}`);
26
}
27
28
//https://firebase.google.com/docs/reference/js/v8/firebase.storage.UploadTask
29
uploadTask.on(
30
"state_changed",
31
//setting loading to true while the file upload is pending
32
(snapshot) => {
33
//to keep the upload button disabled till the upload is completed
34
setLoading(true);
35
console.log(snapshot.state)
36
37
},
38
//function for error
39
(error) => {
40
switch (error.code) {
41
case 'storage/unauthorized':
42
// User doesn't have permission to access the object
43
alert('something went wrong while uploading this file :(')
44
setLoading(false);
45
break;
46
case 'storage/canceled':
47
// User canceled the upload
48
console.log('the cancel method works')
49
setLoading(false);
50
setCancel(false);
51
break;
52
default:
53
alert('something went wrong while uploading this file :(')
54
setLoading(false);
55
}
56
},
57
//function for successful completion
58
() => {
59
setLoading(false);
60
console.log('the upload is successful');
61
}
62
);
63
64
65
};
66
return (
67
<main className="upload">
68
<form onSubmit={formHandler} className="upload-form">
69
<h1>
70
Add a new book for <br /> Everyone to read!
71
</h1>
72
<input
73
type="text"
74
className="gbooks-link"
75
placeholder="Google Books Link"
76
required
77
/>
78
<label className="file-input-label">
79
<input type="file" className="input" id="file-upload" required />
80
Upload the book here
81
</label>
82
<div className="buttons-container">
83
<button disabled={loading} type="submit" className="upload">
84
Upload
85
</button>
86
{
87
loading === true ?
88
<button className="cancel" onClick={() => setCancel(true)}>
89
Cancel
90
</button>
91
:
92
null
93
}
94
95
</div>
96
</form>
97
<img src={bookwall} alt="a wall full of books" className="bookwall" />
98
</main>
99
);
100
};
101
102
export default Upload;
103
104
Here is what’s happening in the console after I click on cancel(after clicking upload and setting a file(img, 3.15mb))
if I just do
JavaScript
1
3
1
const uploadTask = uploadBytesResumable(storageRef, file);
2
console.log(`does upload task cancel work? ${uploadTask.cancel()}`);
3
it returns true and the upload actually gets cancelled
Advertisement
Answer
Turns out it was a scope-related issue…
I had to make the uploadTask
globally available by making it a react state to implement it in my code and I called the uploadTask.cancel()
in my button callback…I used useEffect to monitor my uploadTask and I am doing setLoading(false)
at the button callback instead of the error callback