[英]What's up with this path? Error: "ENOENT: no such file or directory, open
I try to upload an article with image, and the redux devtools telling me the error above.我尝试上传带有图片的文章,redux devtools 告诉我上面的错误。 Obviously it wants to use显然它想使用
C:\xampp\frontside\public\uploads\ C:\xampp\frontside\public\uploads\
The correct path wood be正确的路径木是
C:\xampp\htdocs\mern_redux_ntv\frontside\public\uploads\ C:\xampp\htdocs\mern_redux_ntv\frontside\public\uploads\
The path I have used in my multer-storage is correct.我在多重存储中使用的路径是正确的。 I have tried an absolute path.我尝试了绝对路径。 I tried http://localhost:3000/frontside/public/uploads.我试过 http://localhost:3000/frontside/public/uploads。 I tried it only with /public.我只用 /public 尝试过。 But I have no file in my folder and there is nothing uploaded to cloudinary.但是我的文件夹中没有文件,也没有任何内容上传到 cloudinary。
Here is my backend:这是我的后端:
router.post("/", upload.single("img"), verifyTokenAndAuthorization, async (req,res)=>{
const {img, ressort, theme, title, content} = req.body;
try{
const uploadResult = await cloudinary.uploader.upload(img,{
upload_preset: "Mern_redux-practice",
resource_type: "auto",
}).then ((result)=>{
console.log("Upload successfull", JSON.stringify(result, null, 2));
res.status(200).json(result)
}).catch((error)=>{
console.log("error", JSON.stringify(error, null, 2));
});
Here is my multer-storage:这是我的多用途存储:
const multer = require("multer");
const storage = multer.diskStorage({
destination:(req,file, callback)=>{
callback(null, '../../frontside/public/uploads')
},
fileName: (req, file, callback)=>{
callback(null, Date.now()+ "--"+ file.originalname)
}
})
const upload = multer({storage:storage});
module.exports = upload;
My http-request:我的http请求:
const createMainNews = async (mainnewsData, token)=>{
const config = {
headers: {
// 'Content-Type': `multipart/form-data`,
'Content-Type' : 'application/json',
token: `Bearer ${token}`,
},
}
const response = await axios.post(API_URL, mainnewsData, config);
console.log(response);
return response.data;
}
I found out the solution by myself.我自己找到了解决方案。 It is important to put a static path into the index.js or server.js.将静态路径放入 index.js 或 server.js 很重要。 When you want to store your image in the public folder of the frontend than it is the following:当您想将图像存储在前端的公用文件夹中时,如下所示:
app.use(express.static(path.resolve(process.cwd(), 'frontside/public/')));
And in multer it is:在 multer 中是:
const storage = multer.diskStorage({
destination:(req,file, callback)=>{
callback(null, path.resolve(process.cwd(), 'frontside/public/uploads'));
},
fileName: (req, file, callback)=>{
callback(null, Date.now()+ "--"+ path.extname(file.originalname));
}
})
const upload = multer({storage:storage});
module.exports = upload;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.