0

I am building a blog system (full stack), and I am trying to upload images. The backend cannot find the image that I uploaded because req.file returns the value undefined. Is there any solution to let the files to be successfully uploaded?

My code are below

    router.post("/rich_editor_upload",async(req,res)=>{
    if (!req.files){
        res.send(
            {
                "errno": 1, 
                "message": 
            }
        )
        return;
    } 
    let files = req.files; ///!!!
    let ret_files = [];
    for (let file of files){ 

        let file_ext = file.originalname.substring(file.originalname.lastIndexOf(".")+1)
        let file_name = genid.NextId()+"."+file_ext 

        fs.renameSync(
            process.cwd()+"/public/upload/temp/"+file.file_name, 
            process.cwd()+"/public/upload/"+file_name
        )
        ret_files.push("/upload/"+file_name)

    }

    res.send(
        {
            "errno": 0, 
            "data": {
                "url": ret_files[0], 

            }
        }
    )


})

    module.exports = router  

The error is below

    $ node app.js
Listening to port:8080
node:fs:1042
  handleErrorFromBinding(ctx);
  ^

Error: ENOENT: no such file or directory, rename 'C:\Users\zhang\Desktop\CS 2023\Blog system\Server/public/upload/temp/undefined' -> 'C
:\Users\zhang\Desktop\CS 2023\Blog system\Server/public/upload/468920300437573.jpg'
    at Object.renameSync (node:fs:1042:3)
    at C:\Users\zhang\Desktop\CS 2023\Blog system\Server\routers\UploardRouter.js:40:12
    at Layer.handle [as handle_request] (C:\Users\zhang\Desktop\CS 2023\Blog system\Server\node_modules\express\lib\router\layer.js:95:
5)
    at next (C:\Users\zhang\Desktop\CS 2023\Blog system\Server\node_modules\express\lib\router\route.js:144:13)
    at Route.dispatch (C:\Users\zhang\Desktop\CS 2023\Blog system\Server\node_modules\express\lib\router\route.js:114:3)
    at Layer.handle [as handle_request] (C:\Users\zhang\Desktop\CS 2023\Blog system\Server\node_modules\express\lib\router\layer.js:95:
5)
    at C:\Users\zhang\Desktop\CS 2023\Blog system\Server\node_modules\express\lib\router\index.js:284:15
    at Function.process_params (C:\Users\zhang\Desktop\CS 2023\Blog system\Server\node_modules\express\lib\router\index.js:346:12)     
    at next (C:\Users\zhang\Desktop\CS 2023\Blog system\Server\node_modules\express\lib\router\index.js:280:10)
    at Function.handle (C:\Users\zhang\Desktop\CS 2023\Blog system\Server\node_modules\express\lib\router\index.js:175:3) {
  errno: -4058,
  syscall: 'rename',
  code: 'ENOENT',
  path: 'C:\\Users\\zhang\\Desktop\\CS 2023\\Blog system\\Server/public/upload/temp/undefined',
  dest: 'C:\\Users\\zhang\\Desktop\\CS 2023\\Blog system\\Server/public/upload/468920300437573.jpg'
}

Node.js v18.17.0

I am expecting the files=req.files can get the files that are uploaded

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Wei Zhang
  • 11
  • 2
  • You get `undefined` from `file.file_name`. 1. using an underscore `_` is not usual for JS - accessing a property with an underscore seems like a mistake 2. you already use `file.originalname` to access the name of a file just few lines above. Also seems like a mistake to use `originalname` *and* `file_name` if the two are just the name. – VLAZ Aug 18 '23 at 05:13
  • Also, if `req.files` really returned `undefined` you wouldn't be getting to where the error says you have a problem - the lines *after* looping through the contents of `req.files` and using each entry. – VLAZ Aug 18 '23 at 05:14

1 Answers1

-1

You are using the incorrect slash. You need to use this slash ‘\’.

In the url of the API call need to be as you wrote (‘/‘), and the path in Windows is ‘\’

Please look at this answer: Slashes in path

  • 1
    Thanks... This should not matter... If you try changing the forward slash to backward slash it will still work well... Thanks very much anyway, I didn't notice this – Wei Zhang Aug 18 '23 at 02:32