0

I want to rename a file which I am getting as incoming from client . I am using node js , express , path , fs to perform operations .

Here is the node js function where I want to rename the incoming file :

app.post('/results/upload',(request,response)=>{
    const resultFile = request.files.file;
    const fileInfo="result"
    fs.rename(`${resultFile.name}`, `${fileInfo}`, (err) => {
        if (err) throw err;
        console.log('Rename complete!');
      });

So I am getting error as no such file or directory. its because of the renaming part , so please tell me how to rename the incoming file in node js

This is the log file of the resultFile:

`{
  name: 'Modern React with Redux.pdf',
  data: <Buffer 25 50 44 46 2d 31 2e 32 0a 25 20 63 72 65 61 74 65 64 20 62 79 20 50 49 4c 20 50 44 46 20 64 72 69 76 65 72 20 30 2e 34 0a 31 20 30 20 6f 62 6a 
0a 3c ... 1158630 more bytes>,
  size: 1158680,
  encoding: '7bit',
  tempFilePath: '',
  truncated: false,
  mimetype: 'application/pdf',
  md5: '501b04f153114b7342236a2a185a7ff7',
  mv: [Function: mv]
}`
6
  • Have you tried to specify the full path to the resultFile that you want to rename? Looks like right now you are just specifying the name only and it likely is not in the default searched directory. Your resultDirectory variable is also unused. Perhaps you want to include that in the destination path as well. Commented Jun 6, 2020 at 19:35
  • resultFile is the file which I get from the client , so what is the path for it ? Commented Jun 6, 2020 at 19:36
  • result directory for my other operations Commented Jun 6, 2020 at 19:37
  • Does resultFile not have a path property? Commented Jun 6, 2020 at 19:38
  • 1
    Please provide the output of console.log(resultFile) in your question; Commented Jun 6, 2020 at 19:41

2 Answers 2

1

Looks like you are using: https://www.npmjs.com/package/express-fileupload

You can see in your output that there is a mv function available in your output. Use that function to move the file:

resultFile.mv(newFilePath);

instead of using fs.rename function.

Sign up to request clarification or add additional context in comments.

2 Comments

move the file to where ? anyway , I have a follow up operation with that file to store in my node js server as a separate folder . So I just wanted it to rename the file before that operation for easy accessibility. but If I move the file now it would taking more space right
You can place it wherever you want. That's entirely up to you. 'move' and 'rename' are the same thing in this case. If you want to put it in another folder, specify that in the arguments to the mv function. You can also provide a callback or use the returned promise to do work after it has been placed in that location.
0

You can do fs.rename(), for example:

fs.rename('mynewfile1.txt', 'myrenamedfile.txt', function (err) {
  if (err) throw err;
  console.log('File Renamed!');
});

1 Comment

yea , I did refer the docs , but what is the path for the incoming file , it says no such file or directory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.