Create a NextJS page to upload a CSV, XLS, XLSX file; then process that file in a NextJS…
- User
Create a NextJS page to upload a CSV, XLS, XLSX file; then process that file in a NextJS API route saving the contents to Supabase
- Assistant
Sure, I'd be happy to help you create a Next.js page to upload and process CSV, XLS, and XLSX files, and then save the contents to Supabase. Here's a step-by-step guide to achieve this:
- Install required packages:
First, install the necessary packages for handling file uploads and processing spreadsheets:
bashnpm install --save next-connect formidable @supabase/supabase-js xlsx- Create a custom API route for file upload:
Create a new file
pages/api/upload.jswith the following content:javascriptimport nextConnect from 'next-connect'; import formidable from 'formidable'; import { supabase } from '../../lib/supabase'; import XLSX from 'xlsx'; const handler = nextConnect(); handler.post(async (req, res) => { const form = new formidable.IncomingForm(); form.keepExtensions = true; form.parse(req, async (err, fields, files) => { if (err) { res.status(500).json({ error: 'Error parsing form data' }); return; } const file = files.file; if (!file) { res.status(400).json({ error: 'No file uploaded' }); return; } const workbook = XLSX.readFile(file.path); const sheetName = workbook.SheetNames[0]; const sheet = workbook.Sheets[sheetName]; const data = XLSX.utils.sheet_to_json(sheet); try { await supabase.from('your_table_name').insert(data); res.status(200).json({ message: 'File uploaded and processed successfully' }); } catch (error) { res.status(500).json({ error: 'Error saving data to Supabase' }); } }); }); export const config = { api: { bodyParser: false, }, }; export default handler;Replace with the name of the table in Supabase where you want to save the data.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.