无法在服务器nodeJs上传文件

时间:2017-01-28 17:59:19

标签: javascript node.js express fetch form-data

尝试在客户端上使用fetch()并在服务器上表达+ multer,在节点js服务器上上传pdf文件,查询状态200,但在req.body中有emty对象并且空req.file。请在客户端上查看我的代码。

client.js

fetch(path, conf) {
  let myConf = config;
  return fetch(myConf.apiUrl + path, conf)
    .catch(err => {
      console.warn('api error: ' + err.message)
    })
},
sendForm(data) {
  if (!data) {
    return
  }

  let header, body;

  const fd = new FormData();
  fd.append('file', data.file.files[0])
  header = 'application/pdf';

  body = fd;

  return this
    .fetch('/connect', {
      method: 'POST',
      headers: {
        'Accept': 'application/json, application/xml, text/plain, text/html, *.*',
        'Content-Type': header
      },
      body: body
    })
    .then(res => {
      if (!res.ok) throw new Error('error ' + res.status)
      return res
    })
}

服务器端: server.js

const express = require('express');
const multer = require('multer');
const upload = multer({ dest: './uploads/' });
const app = express();

app.post('/connect', upload.single('resume'), function(req, res, next) {
  res.send({
    body: req.body,
    file: req.file,
    files: req.files
  })
});

邮递员的结果:http://prnt.sc/e1ib8o

0 个答案:

没有答案