Upload files using multer
I was following this gist to setup multer for uploading files but ran into errors, then realized it may be written for the version 0.11.8. I will continue to debug.
Here are the errors:
[ Client!] 9:13:35 PM [vite] Internal server error: Failed to resolve import "@wasp/api" from "../../../src/UploadPage.jsx". Does the file exist?
https://gist.github.com/infomiho/ec379df4e33f3ae3410a251ba3aa81af
4 Replies
It's working now after changing the import per doc: https://wasp-lang.dev/docs/advanced/apis
import { api } from "wasp/client/api";
Is there any way I can supply a custom folder outside .wasp/* ?
.wasp/out/server/uploads
done.
import multer from "multer";
import path from "path";
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "/tmp/tp-uploads");
},
filename: function (req, file, cb) {
const originalName = path.parse(file.originalname).name;
const extension = path.parse(file.originalname).ext;
const timestamp = Date.now();
const newFilename =
${originalName}_${timestamp}${extension};
cb(null, newFilename);
},
});
const upload = multer({ storage: storage });
export const addMiddleware = (config) => {
config.set("multer", upload.single("file"));
return config;
};
export const uploadFile = (req, res, context) => {
console.log(req.body);
console.log(req.file);
const file = req.file;
return res.json({
fileExists: !!file,
});
};
your documentation is 🥇Thank you Jake for the shout out 🫶
I'll update the gist to work with 0.12!
nice @Jake did you access the gist via wasp-lang.dev/docs or OpenSaaS?
or maybe they're the same one
can you drop the link in this open saas issue when you do, Miho? thanks! https://github.com/wasp-lang/open-saas/issues/80