Next.js Discord

Discord Forum

Display Local Images

Answered
Barbary Lion posted this in #help-forum
Open in Discord
Barbary LionOP
Any idea how to solve the error "could not load local resource..."? I'm trying to display an dynamic image by ID.
import { readFileSync } from "node:fs";
import { join } from "node:path";

import BaseParams from "@/types/BaseParams";
import Image from "next/image";

type PageParams = BaseParams<{ imageId: string }>;

function Page({ params }: PageParams) {
    const path = join(process.env.UPLOAD_PATH, params.imageId);

    return (
        <main className="main">
            <h1>{params.imageId}</h1>
            <Image src={path} alt={params.imageId} width={500} height={500} priority={true} />
        </main>
    );
}

export default Page;
Answered by Clown
Yeah, don't try to load files outside the project. Move that file inside your project or upload it online and use the link
View full answer

9 Replies

Print out the process.env.UPLOAD_PATH and that path variable and show me how it looks like
@Clown Print out the process.env.UPLOAD_PATH and that path variable and show me how it looks like
Barbary LionOP
D:\Development\Private\Uploader\uploads\Test.png
Yeah, don't try to load files outside the project. Move that file inside your project or upload it online and use the link
Answer
Barbary LionOP
So there is no way to access that file? I'm making a image uploader, that's why I have to access them dynamically.
Later on, they will be as URL, as I'll upload them to my CDN. But for developing, is there not a way to test it?
If not sure if there is a good way to do it honestly since i have never done it.
@Barbary Lion Later on, they will be as URL, as I'll upload them to my CDN. But for developing, is there not a way to test it?
For development, run a simple http server that acts as your cdn. Afaik due to protocol mismatch, you cannot load file:// URLs in http:// URLs
So while you technically have a url for your file with the file:// protocol, it won’t work
Barbary LionOP
Oh okay, ty