28
loading...
This website collects cookies to deliver better user experience
Remix is a full stack web framework that lets you focus on the user interface and work back through web fundamentals to deliver a fast, slick, and resilient user experience.
export const action = async ({ request }) => {
// get data from form
let form = await request.formData();
let name = form.get("name");
let description = form.get("description");
let state = form.get("state");
// use form information to write to supabase
const { data, error } = await supabaseClient
.from("chargers")
.insert([{ name, description, state }]);
// if no error, back to home page... index.jsx
if (!error) {
return redirect("/", {});
}
// else stay on page and return error information
return { data, error };
};
Currently a bug in Remix when uploading large files. In this example I am only using small files to show how the process works
uploadHandler
which can be found herereplace my bucket id "images" with the name of your bucket
create policy "ALL images are publicly accessible."
on storage.objects for select
using ( bucket_id = 'images' );
create policy "Anyone can upload an image."
on storage.objects for insert
with check ( bucket_id = 'images' );
create policy "Anyone can update an image."
on storage.objects for update
with check ( bucket_id = 'images' );
my-file
export const action = async ({ request }) => {
try {
/**
*
* @param {*} param0
* @returns
*/
let uploadHandler = async ({ name, stream, filename }) => {
console.log("in uploadHandler");
if (name !== "my-file") {
stream.resume();
return;
} else {
console.log(name, filename);
}
// Get the file as a buffer
const chunks = [];
for await (const chunk of stream) chunks.push(chunk);
const buffer = Buffer.concat(chunks);
// call supabase function for uploading to bucket
const { data, error } = await supabaseClient.storage
.from("images")
.upload(filename, buffer);
if (error) {
throw error;
}
// return information up uploaded file
return JSON.stringify({ data });
};
// get file info back after image upload
const form = await unstable_parseMultipartFormData(request, uploadHandler);
//convert it to an object to padd back as actionData
const fileInfo = JSON.parse(form.get("my-file"));
// this is response from upload handler
console.log("the form", form.get("my-file"));
// return success action data
return fileInfo;
} catch (e) {
// return error action data
return { error: e };
}
};