[英]Node.js read multipart/form-data POST
Im passing a file to node.js with AJAX. 我用AJAX将文件传递给node.js。 the file is passed as a Base64 string and I pass it using multipart/form-data. 该文件作为Base64字符串传递,我使用multipart / form-data传递它。 The AJAX part work flawlessly, but I need to have said string stored on a variable in server side ie in Node.js. AJAX部分完美无缺,但我需要将所述字符串存储在服务器端的变量中,即Node.js. Now, on PHP this would be super easy: 现在,在PHP上,这将非常简单:
$someVar = $_POST["myBase64EncodedFile"];
How can I achieve exactly that but with Node.js? 我如何使用Node.js实现这一目标? I have to use Node.js and I dont want the file to be saved in some temp folder, I just need the string on a variabe. 我必须使用Node.js,我不希望文件保存在某个临时文件夹中,我只需要变量上的字符串。 Any ideas? 有任何想法吗? Thanks. 谢谢。
只需使用Request.body
对象:
let someVar = req.body["myBase64EncodedFile"];
use formidable modules. 使用强大的模块。
in express , you can ues it like this: 在快递中,你可以这样:
var formidable = require('formidable');
const form = new formidable.IncomingForm();
function handlerFormByEvent(req, res, next) {
form
.parse(req)
.on('fileBegin', (name, file) => {
console.time('start');
console.log('name', name);
file.path = `uploads/${file.name}`;
})
.on('progress', (accepted, total) => {
console.log(`accept:%d,total:%d`, accepted, total);
})
.on('field', (name, field) => {
console.log('field', name, field);
})
.on('file', (name, file) => {
// handle file
console.log('name', name);
console.log(file.toJSON());
})
.on('aborted', error => {
let message = error.message;
res.render('error', { message, error });
})
.on('error', err => {
console.error('Error', err);
let message = err.message;
res.status(err.status || 500);
res.render('error', { message, error: err });
})
.on('end', () => {
res.end('ok');
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.