Created
November 17, 2022 10:16
-
-
Save ndpniraj/2735c3af00a7c4cbe50602ffe6209fc3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NextApiHandler, NextApiRequest } from "next"; | |
import formidable from "formidable"; | |
import path from "path"; | |
import fs from "fs/promises"; | |
export const config = { | |
api: { | |
bodyParser: false, | |
}, | |
}; | |
const readFile = ( | |
req: NextApiRequest, | |
saveLocally?: boolean | |
): Promise<{ fields: formidable.Fields; files: formidable.Files }> => { | |
const options: formidable.Options = {}; | |
if (saveLocally) { | |
options.uploadDir = path.join(process.cwd(), "/public/images"); | |
options.filename = (name, ext, path, form) => { | |
return Date.now().toString() + "_" + path.originalFilename; | |
}; | |
} | |
options.maxFileSize = 4000 * 1024 * 1024; | |
const form = formidable(options); | |
return new Promise((resolve, reject) => { | |
form.parse(req, (err, fields, files) => { | |
if (err) reject(err); | |
resolve({ fields, files }); | |
}); | |
}); | |
}; | |
const handler: NextApiHandler = async (req, res) => { | |
try { | |
await fs.readdir(path.join(process.cwd() + "/public", "/images")); | |
} catch (error) { | |
await fs.mkdir(path.join(process.cwd() + "/public", "/images")); | |
} | |
await readFile(req, true); | |
res.json({ done: "ok" }); | |
}; | |
export default handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { GetServerSideProps, NextPage } from "next"; | |
import { useState } from "react"; | |
import axios from "axios"; | |
import fs from "fs/promises"; | |
import path from "path"; | |
import Link from "next/link"; | |
interface Props { | |
dirs: string[]; | |
} | |
const Home: NextPage<Props> = ({ dirs }) => { | |
const [uploading, setUploading] = useState(false); | |
const [selectedImage, setSelectedImage] = useState(""); | |
const [selectedFile, setSelectedFile] = useState<File>(); | |
const handleUpload = async () => { | |
setUploading(true); | |
try { | |
if (!selectedFile) return; | |
const formData = new FormData(); | |
formData.append("myImage", selectedFile); | |
const { data } = await axios.post("/api/image", formData); | |
console.log(data); | |
} catch (error: any) { | |
console.log(error.response?.data); | |
} | |
setUploading(false); | |
}; | |
return ( | |
<div className="max-w-4xl mx-auto p-20 space-y-6"> | |
<label> | |
<input | |
type="file" | |
hidden | |
onChange={({ target }) => { | |
if (target.files) { | |
const file = target.files[0]; | |
setSelectedImage(URL.createObjectURL(file)); | |
setSelectedFile(file); | |
} | |
}} | |
/> | |
<div className="w-40 aspect-video rounded flex items-center justify-center border-2 border-dashed cursor-pointer"> | |
{selectedImage ? ( | |
<img src={selectedImage} alt="" /> | |
) : ( | |
<span>Select Image</span> | |
)} | |
</div> | |
</label> | |
<button | |
onClick={handleUpload} | |
disabled={uploading} | |
style={{ opacity: uploading ? ".5" : "1" }} | |
className="bg-red-600 p-3 w-32 text-center rounded text-white" | |
> | |
{uploading ? "Uploading.." : "Upload"} | |
</button> | |
<div className="mt-20 flex flex-col space-y-3"> | |
{dirs.map((item) => ( | |
<Link key={item} href={"/images/" + item}> | |
<a className="text-blue-500 hover:underline">{item}</a> | |
</Link> | |
))} | |
</div> | |
</div> | |
); | |
}; | |
export const getServerSideProps: GetServerSideProps = async () => { | |
const props = { dirs: [] }; | |
try { | |
const dirs = await fs.readdir(path.join(process.cwd(), "/public/images")); | |
props.dirs = dirs as any; | |
return { props }; | |
} catch (error) { | |
return { props }; | |
} | |
}; | |
export default Home; |
I struggled two days for storing images. I used several ways like multer and other but multer was not working as expected. Your tutorial gave me right direction. Thanks God bless you.
Bruh you the best
"Great work, brother! You've really outdone yourself."
I had given full two days, still it could not be done but your code worked...
๐ ๐ท๐๐๐ ๐๐ฎ๐ป๐ ๐๐ผ ๐ธ๐ป๐ผ๐ ๐๐ต๐ถ๐ฐ๐ต ๐ฟ๐ฒ๐๐ผ๐๐ฟ๐ฐ๐ฒ(documentation) ๐๐ผ๐ ๐๐๐ฒ๐ฑ, ๐ฐ๐ฎ๐ป ๐๐ผ๐ ๐ฝ๐น๐ ๐๐ต๐ฎ๐ฟ๐ฒ ?
Your code looks good, I will try it right now!
Ok LGTM, Thanks
Thank you i have done a lot of things to get this working seeing how easy i can do that in dotnet
Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've tried so many different things, but this was the only one that works. Thank you!