Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在ExpressJS中从post请求写入文件_Node.js_Express_Requestjs - Fatal编程技术网

Node.js 在ExpressJS中从post请求写入文件

Node.js 在ExpressJS中从post请求写入文件,node.js,express,requestjs,Node.js,Express,Requestjs,与express合作创建了一个文件传输工具,我几乎完成了所有工作。只需要弄清楚如何从写入文件的请求中获取数据 我的问题似乎源于不知道文件内容在请求对象中的位置 处理发送请求的我的代码 let file = watcher.getOneFile(config.thisLocation); console.dir(file); let contents = fs.readFileSync(file.fullPath, 'utf-8'); console.log(contents); let form

与express合作创建了一个文件传输工具,我几乎完成了所有工作。只需要弄清楚如何从写入文件的请求中获取数据

我的问题似乎源于不知道文件内容在请求对象中的位置

处理发送请求的我的代码

let file = watcher.getOneFile(config.thisLocation);
console.dir(file);
let contents = fs.readFileSync(file.fullPath, 'utf-8');
console.log(contents);
let form = {
  attachments: [
    contents
  ]
}
rq.post({
  url: `http://${homeAddress}:${port}/files/?loc=${config.thisLocation}&file=${file.fileName}`,
  headers: {'content-type':'application/x-www-form-urlencoded'},
  formData: form
}, (err, res, body) => {
  // body = JSON.parse(body);
  console.log(body);
});
app.post('/files', (req, res) => {
  console.log(req.query.loc);
  // console.dir(req);
  let incoming = watcher.getOutputPath(req.query.loc, config.locations);
  console.log(incoming);
  console.dir(req.body);
  // console.log(req.body);
  // let body = JSON.parse(req.body);
  console.log(req.query);
  let filename = path.join(incoming, req.query.file);
  console.log(filename);
  fs.writeFile(filename, req.body, (err) => {
    if(err){
      console.error(err);
    }
    console.log(`Successfully wrote file: ${path.join(incoming, req.query.file)}`);
  });
  res.sendStatus(200);
});
当我在服务器上收到请求时,我不确定文件内容实际在哪里

处理请求的代码

let file = watcher.getOneFile(config.thisLocation);
console.dir(file);
let contents = fs.readFileSync(file.fullPath, 'utf-8');
console.log(contents);
let form = {
  attachments: [
    contents
  ]
}
rq.post({
  url: `http://${homeAddress}:${port}/files/?loc=${config.thisLocation}&file=${file.fileName}`,
  headers: {'content-type':'application/x-www-form-urlencoded'},
  formData: form
}, (err, res, body) => {
  // body = JSON.parse(body);
  console.log(body);
});
app.post('/files', (req, res) => {
  console.log(req.query.loc);
  // console.dir(req);
  let incoming = watcher.getOutputPath(req.query.loc, config.locations);
  console.log(incoming);
  console.dir(req.body);
  // console.log(req.body);
  // let body = JSON.parse(req.body);
  console.log(req.query);
  let filename = path.join(incoming, req.query.file);
  console.log(filename);
  fs.writeFile(filename, req.body, (err) => {
    if(err){
      console.error(err);
    }
    console.log(`Successfully wrote file: ${path.join(incoming, req.query.file)}`);
  });
  res.sendStatus(200);
});

请求对象上的哪里是文件内容?

不幸的是,您无法以任何直接的方式访问文件内容。我建议您使用或类似的包来解析表单数据请求

以下是如何使用
busboy
读取文件内容并将其写入文件系统:

const Busboy = require('busboy');

app.post('/files', (req, res) => {
  const busboy = new Busboy({ headers: req.headers });

  busboy.on('file', (fieldname, file, filename, encoding, mime) => {
    const newFilename = `${Date.now()}_${filename}`,
      newFile = fs.createWriteStream(newFilename);

    file.pipe(newFile);

    file.on('end', () => {
      console.log(`Finished reading ${filename}`);
    });
  });

  busboy.on('finish', () => {
    console.log('Finished parsing form');

    res.sendStatus(200);
  });

  req.pipe(busboy);
});

您在记录请求文件时是否尝试过
req.file
req.files
,它们未定义。谢谢您的帮助,。我会试试这个。谢谢你更新你的答案。不客气。如果您对此有任何问题,请发布。只需与邮递员一起测试,就可以了!现在使用我的其他代码进行测试!是的,为我的用例做了一些修改!谢谢