Uploading files from the frontend lets users send pictures, documents, or other files to your app. This helps store and use user data easily.
Uploading files from frontend in Supabase
const { data, error } = await supabase.storage
.from('bucket-name')
.upload('path/to/file.ext', fileObject);supabase.storage.from('bucket-name') selects where to store the file.
.upload('path/to/file.ext', fileObject) uploads the file to that path.
profile.png for user 123 into the avatars bucket.const { data, error } = await supabase.storage
.from('avatars')
.upload('user123/profile.png', file);report1.pdf into the documents bucket.const { data, error } = await supabase.storage
.from('documents')
.upload('reports/report1.pdf', file);This example shows how to upload a file selected by the user from an HTML file input. It uploads the file to the user-files bucket under the uploads/ folder. If successful, it logs and alerts the file path.
import { createClient } from '@supabase/supabase-js'; const supabaseUrl = 'https://xyzcompany.supabase.co'; const supabaseKey = 'public-anonymous-key'; const supabase = createClient(supabaseUrl, supabaseKey); async function uploadFile(file) { const filePath = `uploads/${file.name}`; const { data, error } = await supabase.storage .from('user-files') .upload(filePath, file); if (error) { console.error('Upload error:', error.message); return null; } console.log('File uploaded at:', data.path); return data.path; } // Example usage with a file input element const fileInput = document.querySelector('#fileInput'); fileInput.addEventListener('change', async (event) => { const file = event.target.files[0]; if (file) { const path = await uploadFile(file); alert(`File uploaded to: ${path}`); } });
Make sure the bucket (user-files in the example) exists in your Supabase storage.
File names should be unique or you risk overwriting existing files.
Check for errors to handle upload failures gracefully.
Uploading files from frontend lets users send files to your app storage.
Use supabase.storage.from(bucket).upload(path, file) to upload.
Always handle errors and confirm uploads to improve user experience.
Practice
supabase.storage.from(bucket).upload(path, file) method do in a frontend app?Solution
Step 1: Understand the method purpose
The methodupload(path, file)is used to send a file from the frontend to the backend storage.Step 2: Identify the action on storage
Since it usesupload, it means adding or saving a file, not deleting or downloading.Final Answer:
Uploads a file to the specified storage bucket at the given path -> Option BQuick Check:
Upload method = Upload file [OK]
- Confusing upload with download or delete
- Thinking it lists files instead of uploading
- Ignoring the bucket and path parameters
myFile to the bucket avatars at path user1/profile.png using Supabase storage in frontend JavaScript?Solution
Step 1: Check method chaining and parameters
The correct method chain issupabase.storage.from(bucket).upload(path, file).Step 2: Match parameters order
The first argument touploadis the path string, second is the file object.Final Answer:
supabase.storage.from('avatars').upload('user1/profile.png', myFile) -> Option AQuick Check:
Correct method and parameter order = supabase.storage.from('avatars').upload('user1/profile.png', myFile) [OK]
- Swapping file and path arguments
- Using upload directly on storage without from()
- Incorrect method chaining order
const { data, error } = await supabase.storage.from('docs').upload('file.txt', fileInput.files[0]);
if (error) {
console.log('Upload failed');
} else {
console.log('Upload succeeded:', data);
}Solution
Step 1: Understand the upload response
On success,datacontains info about the uploaded file, including its path.Step 2: Check error handling
Iferroris null, the else block runs and logs success withdata.Final Answer:
Upload succeeded: { path: 'file.txt', ... } -> Option AQuick Check:
Success logs data object = Upload succeeded: { path: 'file.txt', ... } [OK]
- Assuming data is undefined on success
- Confusing error presence with success
- Thinking await causes syntax error outside async
const { data, error } = supabase.storage.from('images').upload('pic.png', file);
if (error) {
console.log('Upload failed');
} else {
console.log('Upload succeeded');
}Solution
Step 1: Check async usage
Theuploadmethod returns a promise and must be awaited to getdataanderror.Step 2: Identify missing await effect
Withoutawait,{ data, error }destructures the unresolved Promise, leaving bothundefined. Thusif (error)is false, incorrectly logging success.Final Answer:
Missing await before supabase.storage.from(...).upload(...) -> Option CQuick Check:
Async calls need await to get results [OK]
- Not awaiting the upload promise
- Assuming upload is synchronous
- Ignoring file variable existence
uploads and keep their original filenames. Which approach correctly uploads all files and handles errors properly?Solution
Step 1: Understand multiple file uploads
Supabase storageuploadhandles one file at a time, so loop over files.Step 2: Handle async and errors properly
Useawaitinside the loop to wait for each upload and check for errors to inform the user.Final Answer:
Use a loop with await inside to upload each file and check errors after each upload -> Option DQuick Check:
Upload files one by one with await and error check [OK]
- Passing array directly to upload method
- Not awaiting uploads causing race conditions
- Overwriting files by using same filename
Ready to prove it?
Pass the test and this pattern moves unknown
