Uploading Files to a Backend API
Use FormData and fetch to POST files and track upload progress.
Uploading Files to a Backend API is a free React Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Welcome
FormData API
const formData = new FormData();
formData.append('file', selectedFile);
formData.append('description', 'Profile photo');Uploading with fetch
const response = await fetch('/api/upload', {
method: 'POST',
body: formData,
// Do not set Content-Type header here!
});Upload State Management
const [uploading, setUploading] = useState(false);
const [progress, setProgress] = useState(0);
const [error, setError] = useState(null);Upload Progress with XMLHttpRequest
const xhr = new XMLHttpRequest();
xhr.upload.onprogress = (e) => {
if (e.lengthComputable) {
setProgress(Math.round((e.loaded / e.total) * 100));
}
};
xhr.open('POST', '/api/upload');
xhr.send(formData);Wrapping XHR in a Promise
function uploadFile(file) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.onload = () => resolve(xhr.response);
xhr.onerror = () => reject(new Error('Upload failed'));
xhr.upload.onprogress = (e) => setProgress(Math.round(e.loaded / e.total * 100));
xhr.open('POST', '/api/upload');
xhr.send(new FormData().set && (() => { const f = new FormData(); f.append('file', file); xhr.send(f); })());
});
}Handling the Server Response
const res = await fetch('/api/upload', { method: 'POST', body: fd });
if (!res.ok) throw new Error('Upload failed');
const { url } = await res.json();
setUploadedUrl(url);Error Handling
try {
setUploading(true);
await uploadToServer(file);
} catch (err) {
setError(err.message);
} finally {
setUploading(false);
}Cancelling an Upload
const abortRef = useRef();
const start = async () => {
abortRef.current = new AbortController();
await fetch('/api/upload', { body: fd, signal: abortRef.current.signal });
};
const cancel = () => abortRef.current?.abort();Multiple File Uploads
Quick Check
Recap
Course Complete
Frequently asked questions
Is the “Uploading Files to a Backend API” lesson free?
Yes — the full text of “Uploading Files to a Backend API” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.
What will I learn in “Uploading Files to a Backend API”?
Use FormData and fetch to POST files and track upload progress. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start React Academy?
No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Uploading Files to a Backend API” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this React Academy lesson?
Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Uncontrolled Inputs with useRef
- File Input & Reading File Data
- Drag & Drop File Upload UI
- Uploading Files to a Backend API