如何从koa2中的多部分数据解析流
How to parse stream from multipart data in koa2
所以我在多部分表单数据中上传流数据
var Koa = require('koa');
var Router = require('koa-router');
const HttpStatus = require('http-status');
const fs = require('fs');
const koaBody = require('koa-body');
const request = require('request');
var FormData = require('form-data');
var formData = {
my_field: 'my_value',
my_file: fs.createReadStream('robot.jpg'),
};
request.post({url:'http://localhost:3456/fileStorage', formData: formData}, function(err, httpResponse, body) {
if (err) {
return console.error('upload failed:', err);
}
console.log('Upload successful! Server responded with:', body);
});
在服务器端我尝试了koa-multer,尝试访问文件时出错。只需使用普通的 koa-body,我可以看到文件已上传,但无法将其保存回文件,请提供任何提示?
{ my_file:
File {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
size: 15562,
path: '/var/folders/l6/lmwkwk196_qfxxngv5ys0lp00000gn/T/upload_fc48a19e6ee5abf8a9a361d784725b78',
name: 'robot.jpg',
type: 'image/jpeg',
...
}
来源是这个
var Koa = require('koa');
var Router = require('koa-router');
const HttpStatus = require('http-status');
const fs = require('fs');
const koaBody = require('koa-body');
//const multiparty = require('multiparty');
//const multer = require('koa-multer');
var app = new Koa();
var router = new Router();
//const upload=multer({desk: './uploads/' });
router
.post(`/upload`, koaBody({multipart:true}),
(ctx) => {
console.log('----- hello ----');
console.log(ctx.request.body.files);
console.error('---------------');
const writeStream = fs.createWriteStream('./111.jpg');
ctx.request.body.files.my_file.pipe(writeStream);
});
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(3456);
好的,我刚刚做了这个
const readStream = fs.createReadStream(ctx.request.body.files.my_file.path);
所以我可以从 http post.
接收多部分的文件流
所以我在多部分表单数据中上传流数据
var Koa = require('koa');
var Router = require('koa-router');
const HttpStatus = require('http-status');
const fs = require('fs');
const koaBody = require('koa-body');
const request = require('request');
var FormData = require('form-data');
var formData = {
my_field: 'my_value',
my_file: fs.createReadStream('robot.jpg'),
};
request.post({url:'http://localhost:3456/fileStorage', formData: formData}, function(err, httpResponse, body) {
if (err) {
return console.error('upload failed:', err);
}
console.log('Upload successful! Server responded with:', body);
});
在服务器端我尝试了koa-multer,尝试访问文件时出错。只需使用普通的 koa-body,我可以看到文件已上传,但无法将其保存回文件,请提供任何提示?
{ my_file:
File {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
size: 15562,
path: '/var/folders/l6/lmwkwk196_qfxxngv5ys0lp00000gn/T/upload_fc48a19e6ee5abf8a9a361d784725b78',
name: 'robot.jpg',
type: 'image/jpeg',
...
}
来源是这个
var Koa = require('koa');
var Router = require('koa-router');
const HttpStatus = require('http-status');
const fs = require('fs');
const koaBody = require('koa-body');
//const multiparty = require('multiparty');
//const multer = require('koa-multer');
var app = new Koa();
var router = new Router();
//const upload=multer({desk: './uploads/' });
router
.post(`/upload`, koaBody({multipart:true}),
(ctx) => {
console.log('----- hello ----');
console.log(ctx.request.body.files);
console.error('---------------');
const writeStream = fs.createWriteStream('./111.jpg');
ctx.request.body.files.my_file.pipe(writeStream);
});
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(3456);
好的,我刚刚做了这个 const readStream = fs.createReadStream(ctx.request.body.files.my_file.path);
所以我可以从 http post.
接收多部分的文件流