Not works fs.readFile in node js

后端 未结 5 801

I have:

 fs.readFile(\'../services/Prescipcion.xml\', \"utf8\", function (err, data) {
    console.log(\"err->\", err);
    console.log(\"data\", data);
 });
         


        
相关标签:
5条回答
  • 2021-02-19 10:57

    When passing data over from a Web App to an Express server, the fs root resides under the Web App directory and NOT the server's root directory. Therefore, the first writeFile parameter must point to a directory outside the server directory, or link over to the server file tree.

    0 讨论(0)
  • 2021-02-19 11:03

    The error message says no such file or directory, so at first sight this most likely means the path to the file is incorrect.

    Either the filename is incorrect (typo?) or the directory is incorrectly resolved. Take note that a relative path will be resolved against process.cwd():

    process.cwd(): Returns the current working directory of the process.

    You can try using console.log(process.cwd()) to help you debug the issue.

    If the file Prescipcion.xml should be retrieved locally from where the script is run, you can also use the following construct:

    fs.readFileSync(path.join(__dirname, '../services') + '/Prescipcion.xml', 'utf8');

    __dirname: The name of the directory that the currently executing script resides in.

    0 讨论(0)
  • 2021-02-19 11:07

    you can use relative or absolute path as fs.readFile argument , but the point is that i got stumped with this was:

    if you have let path = 'c:\a\b\c.d and you want to use it as an arg for fs.readFile(path, {'utf-8'}, (err, content) => {//...your stuffs}) you should take care that \ is an escape character in js, so fs read your path as c:abc.d then it will use this path as a relative path so fs will search for C:\Users\dir_to_your_project\c:abc.d which is obviously not a valid path and it results in a Error: ENOENT: no such file or directory... error

    so how to fix this issue?

    it's very simple you should just replace \ with \\ in your desired path before using it as an arg in fs.readFile():

    new_path_for_fsReadFileArg = path.replace(/\\/g,"\\\\");
    
    0 讨论(0)
  • 2021-02-19 11:15

    A full example that worked for me, based on other answer

    var fs = require('fs');
    var path = require('path');
    var readStream = fs.createReadStream(path.join(__dirname, '../rooms') + '/rooms.txt', 'utf8');
    let data = ''
    readStream.on('data', function(chunk) {
        data += chunk;
    }).on('end', function() {
        console.log(data);
    });
    
    0 讨论(0)
  • 2021-02-19 11:17

    It worked for me

      var fs = require("fs");
    
        const readFIle = path => {
          fs.readFile(__dirname + path, "utf8", (err, data) => {
            if (err) {
              console.log(err.stack);
              return;
            }
            console.log(data.toString());
          });
          console.log("Program Ended");
        };
    

    usage:

    readFIle("/input.txt");

    0 讨论(0)
提交回复
热议问题