I want to get the file from an api route, but how do i send the file from api route, as its an array buffer and i cant send it using NextResponse.json()
I haven't worked with files, this is my first time
TL;DR
There is a misconception here. You should not send the file in the JSON response since it's an array buffer. Instead of using `NextResponse.json()`, you can use `NextResponse.buffer()` to return the file content. This way, the users can download the file when they access your API route.My API Route Code
TypeScript
import { SESSION_COOKIE } from "@/const";
import { db, noteAttachmentBucket, noteCollection } from "@/models/name";
import { createSessionClient } from "@/models/server/config";
import { NextRequest, NextResponse } from "next/server";
export async function GET(req: NextRequest) {
try {
const note_id = req.nextUrl.searchParams.get("note_id")
if (!note_id) {
return NextResponse.json({
success: false,
error: "Note ID is required"
}, { status: 400 })
}
const session = req.cookies.get(SESSION_COOKIE)
if (!session || !session.value) {
return NextResponse.json({
success: false,
error: "Unauthorized"
}, { status: 401 })
}
const { databases, storage } = await createSessionClient(session)
const note = await databases.getDocument(db, noteCollection, note_id)
if (!note) {
return NextResponse.json({
success: false,
error: "Note not found"
}, { status: 404 })
}
const viewFile = await storage.getFileView(noteAttachmentBucket, note.file_id)
return NextResponse.json({
success: true,
noteData: {
title: note.title,
description: note.description,
subject: note.subject,
tags: note.tags,
}
})
}
catch (err) {
return NextResponse.json({
success: false,
error: "Internal server error"
}, { status: 500 })
}
}
Recommended threads
- Cannot migrate from Supabase
Hi, I was trying to migrate from Supabase to the Appwrite database. My database had around 190K columns. I tried it twice. Both times, after 2 days, the proce...
- Production down - createExecution return...
Hey, Since 1.9.6 rollout in fra (~17:00 UTC, 14 Aug), POST /v1/functions/{functionId}/executions returns a 400 whenever the body includes an empty headers ob...
- Domain is owned by a different organizat...
Hi Appwrite team, I'm trying to add my custom domain **futlive9.com** to my project, but I'm getting the error: "Domain is owned by a different organization." ...