简体   繁体   English

这条路怎么了? 错误:“ENOENT:没有这样的文件或目录,打开

[英]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.

相关问题 错误:ENOENT:没有那个文件或目录,打开 - Error: ENOENT: no such file or directory, open Heroku错误:ENOENT:没有此类文件或目录,请打开“ .env” - Heroku Error: ENOENT: no such file or directory, open '.env' 详细堆栈错误:ENOENT:没有这样的文件或目录,打开 - verbose stack Error: ENOENT: no such file or directory, open 错误 UNHANDLED REJECTION ENOENT: 没有这样的文件或目录,打开 '/vercel/path0/.cache/json/ - error UNHANDLED REJECTION ENOENT: no such file or directory, open '/vercel/path0/.cache/json/ 错误:ENOENT:没有这样的文件或目录,在 Docker 容器上运行时打开“/root/.aws/credentials” - Error: ENOENT: no such file or directory, open '/root/.aws/credentials' when run up on Docker Container 错误:ENOENT:没有这样的文件或目录,打开尝试使用 fs 访问目录时 - Error: ENOENT: no such file or directory, open When trying to access a directory with fs 错误:否:没有此类文件或目录,请打开“ ./views/s.ejs”Nodejs Express - Error: ENOENT: no such file or directory, open './views/s.ejs' Nodejs Express 错误:ENOENT:没有这样的文件或目录 - Error: ENOENT: no such file or directory 如何解决writeFile()的[错误:ENOENT:没有这样的文件或目录,打开] - How to solve [Error: ENOENT: no such file or directory, open ] for writeFile() 如何修复 nodejs 抛出错误; ^ 错误:ENOENT:没有这样的文件或目录,打开 - How to fix nodejs throw err; ^ Error: ENOENT: no such file or directory, open
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM