gpt4 book ai didi

node.js - 上传文件时获取 API 和 multer 错误

转载 作者:IT老高 更新时间:2023-10-28 23:23:30 29 4
gpt4 key购买 nike

我正在尝试使用 fetch API 将文件上传到 node.js 服务器(如果与它有关,我正在使用 github 的 pollyfill: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,
});

在我的服务器端,这个路由声明:

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 没有被填充。

我也从 express 方面收到此错误:

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)

这是通过中间件进行的配置:

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 一起正常工作。所以,我认为我的请求有问题。

谢谢!

最佳答案

对于那些在使用 postman 时遇到错误的人,请确保不要在标题中指定内容类型

关于node.js - 上传文件时获取 API 和 multer 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35795529/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com