文件没有得到保存,而使用multer, MEAN堆栈

file not getting saved while using multer, MEAN stack

本文关键字:multer MEAN 堆栈 文件 保存      更新时间:2023-09-26

我正在构建一个节点应用程序,可以保存使用FormData()发送的文件。我用的是多模块。在FormData中,我使用XPCOM组件发送文件,该组件是FileUtils。

客户端代码

    var file = FileUtils.getFile("AChrom", [att.name]);
    form.append("file", file);
    xhr.send(form); // Object of XMLHTTPRequest

这是我的request.headers

{ host: 'localhost:3000',
  'user-agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0',
  accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  'accept-language': 'en-US,en;q=0.5',
  'accept-encoding': 'gzip, deflate',
  'content-length': '1111',
  'content-type': 'multipart/form-data; boundary=---------------------------12849261213577024421216708977',
  connection: 'keep-alive' }

这是我的请求。身体

  { messageId:    'xyz@yahoo.com',
  subject: 'Testing pdf',
  date: '1473703752000000',
  author: 'xyz',
  mBody: 'welcome to the world of dreamers',
  file: '[xpconnect wrapped nsIFile]',
  fileName: 'Boot Camp.pdf' }

我在服务器上的代码

var multer = require('multer');
var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, '.')
  },
  filename: function (req, file, cb) {
    cb(null, req.body.fileName)
  }
});
var upload = multer({storage: storage});
app.post('/', upload.single('file'),  function(req, res) {
  console.log(req.body);
  res.end("thank you");
})

我在SO上找到了一个类似的帖子,但是我不知道我的代码出了什么问题。

用客户端代码编辑解析

// var file = FileUtils.getFile("AChrom", [att.name]); // this just returns an file object not the actual file
var attachmentFile = new File(path)
form.append("file", file);
xhr.send(form);