Is there a way to send a file from either route.ts or page.ts, regardless of its location in the file-system?
Currently, I am using the following code in my back-end python + flask
...
@app.route("/thumbnail/<string:filename>")
def get_file(filename):
pathname = os.path.join(session['uploadpath'], filename)
return send_file(pathname)
This code allows me to deliver images to the browser based on the HTML+jinja
code...
<img src="/thumbnail/{{file}}" style="height:40px"/>
It works for delivering images from different locations in the file-system.
In addition, I can create an image in memory and send it as a file without saving it...
@app.route("/textimg")
def get_file(filename):
img = Image.new('RGB',(120,40), color='red')
fnt = ImageFont.truetype(app.root_path + '/FONTNAME.TTF', 36)
d = ImageDraw.Draw(img)
d.text((10,0), '123456', font=fnt, fill=(255,255,0))
img_io = BytesIO()
img.save(img_io, 'JPEG', quality=70)
img_io.seek(0)
return send_file(img_io, mimetype='image/jpeg')
Now, I need to transition to nextjs as per the client's request. However, I am struggling to find the equivalent of send_file
. In nextjs, accessing files outside the public
folder is not straightforward.
I would appreciate assistance in resolving this issue - How can I implement send_file functionality in nextjs?
Some methods I have attempted include...
res.setHeader('Content-Type', 'image/jpg')
const imageStream = createReadStream(file)
pipeline(imageStream, res, (error) => {
console.log(error);
})
and
import fs from 'fs'
import path from 'path'
const filePath = path.resolve('.', 'images_folder/next.jpg')
const imageBuffer = fs.readFileSync(filePath)
export default function(req, res) {
res.setHeader('Content-Type', 'image/jpg')
res.send(imageBuffer)
}
and
import { ImageResponse } from "next/server";
export default async function get_file(req, res) {
const filename = req.query.filename;
const pathname = os.path.join(session['uploadpath'], filename);
const imageResponse = new ImageResponse(pathname);
imageResponse.headers.set("Content-Type", "image/png");
return imageResponse;
}
I have come across these code snippets through research but...