问题描述
我正在尝试使用访存API将文件上传到node.js服务器(如果与GitHub的pollyfill相关,则我正在使用github的pollyfill: https://github.com/github/fetch ).
I'm trying to use fetch API to upload files to a node.js server (I'm using github's pollyfill if it has something to do with it: https://github.com/github/fetch).
请求是这样完成的:
const data = new FormData();
data.append('file', file);
return fetch(this.concatToUrl(url), {
method: 'post',
headers: Object.assign({}, this.getHeaders(), {'Content-Type': 'multipart/form-data'}),
body: data,
});
在服务器端,我有以下路由声明:
On the server side I have, this declaration of route:
app.post('/media', upload.single('file'), mediaRoutes.postMedia);
并尝试获取如下文件:
exports.postMedia = function(req, res) {
console.log('req.file', req.file, req.files, req.body);
return res.sendStatus(200);
}
但是req.file
未被填充.
我也从快递方面得到了这个错误:
Also I'm getting this error from express side:
Error: Multipart: Boundary not found
[2] at new Multipart (/Users/jmanzano/Development/web/test/node_modules/busboy/lib/types/multipart.js:58:11)
[2] at Multipart (/Users/jmanzano/Development/web/test/node_modules/busboy/lib/types/multipart.js:26:12)
[2] at Busboy.parseHeaders (/Users/jmanzano/Development/web/test/node_modules/busboy/lib/main.js:64:22)
[2] at new Busboy (/Users/jmanzano/Development/web/test/node_modules/busboy/lib/main.js:21:10)
[2] at multerMiddleware (/Users/jmanzano/Development/web/test/node_modules/multer/lib/make-middleware.js:32:16)
[2] at Layer.handle [as handle_request] (/Users/jmanzano/Development/web/test/node_modules/express/lib/router/layer.js:95:5)
[2] at next (/Users/jmanzano/Development/web/test/node_modules/express/lib/router/route.js:131:13)
[2] at Route.dispatch (/Users/jmanzano/Development/web/test/node_modules/express/lib/router/route.js:112:3)
[2] at Layer.handle [as handle_request] (/Users/jmanzano/Development/web/test/node_modules/express/lib/router/layer.js:95:5)
[2] at /Users/jmanzano/Development/web/test/node_modules/express/lib/router/index.js:277:22
[2] at Function.process_params (/Users/jmanzano/Development/web/test/node_modules/express/lib/router/index.js:330:12)
[2] at next (/Users/jmanzano/Development/web/test/node_modules/express/lib/router/index.js:271:10)
[2] at cors (/Users/jmanzano/Development/web/test/node_modules/cors/lib/index.js:178:7)
[2] at /Users/jmanzano/Development/web/test/node_modules/cors/lib/index.js:228:17
[2] at originCallback (/Users/jmanzano/Development/web/test/node_modules/cors/lib/index.js:217:15)
[2] at /Users/jmanzano/Development/web/test/node_modules/cors/lib/index.js:222:13
[2] at optionsCallback (/Users/jmanzano/Development/web/test/node_modules/cors/lib/index.js:203:9)
[2] at /Users/jmanzano/Development/web/test/node_modules/cors/lib/index.js:208:7
[2] at Layer.handle [as handle_request] (/Users/jmanzano/Development/web/test/node_modules/express/lib/router/layer.js:95:5)
[2] at trim_prefix (/Users/jmanzano/Development/web/test/node_modules/express/lib/router/index.js:312:13)
[2] at /Users/jmanzano/Development/web/test/node_modules/express/lib/router/index.js:280:7
[2] at Function.process_params (/Users/jmanzano/Development/web/test/node_modules/express/lib/router/index.js:330:12)
这是通过中间件进行的配置:
And this is the configuration via middlewares:
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(expressValidator());
app.use(logger('dev'));
app.use(cookieParser());
app.use(methodOverride());
app.use(passport.initialize());
app.use(passport.session());
app.set('JWTSuperSecret', jwtConfig.secret);
if (process.env.NODE_ENV !== 'production') {
app.use(cors());
}
此外,这在POSTMAN上也可以正常使用.所以,我认为我的请求做错了.
Also, this is correctly working with POSTMAN. So, I think I'm doing something wrong with the request.
谢谢!
推荐答案
对于绊倒这篇文章的人,如果在使用邮递员时出现错误 ,请确保不指定内容类型.
For those stumbling upon this post who have the error when using postman make sure to not specify the content-type in the headers.
这篇关于上载文件时获取API和multer错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!