var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
if (err) {
res.status(200).send('parsing error');
}
});
var jsonObject;
var imageArray = [];
form.on('file', function(field, file) {
console.log(file);
if (file.type == 'application/json') {
jsonObject = JSON.parse(fs.readFileSync(file.path, 'utf8'));
} else if (file.type == 'image/jpeg') {
imageArray.push(file.path);
}
})
I'm using formidable to handle a multipart request in node.js made from my iOS app, the request usually contains "n" number of images and a associated JSON object, the formidable middleware treats the JSON as an file and stores the file in the temporary download location and provides a path to the file, the code "jsonObject = JSON.parse(fs.readFileSync(file.path, 'utf8'));" reads the file and parses into a JSON object. Is this how it is supposed to work or is there a better way, that negates the superfluous file write and read from the disk and provides a parsed JSON obj directly.
PS - newbie to node.js, heck newbie to entire backend system with a very bad case OCD.
如果在Content-Disposition
标头值中设置了文件filename
(或者如果Content-Type
为application/octet-stream
则可能如此),强大(几乎所有其他节点形式的解析模块)将仅将特定部分视为文件。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.