本文整理汇总了TypeScript中multiparty.Form类的典型用法代码示例。如果您正苦于以下问题:TypeScript Form类的具体用法?TypeScript Form怎么用?TypeScript Form使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Form类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: parseRequestData
export function parseRequestData( req: http.ServerRequest, contentType: string ) : Q.Promise<any> {
var log = _log.child( { func: 'internals.parseData'} );
var later = Q.defer< any >();
var mimeType = contentType.split(/[\s,;]+/)[0];
if ( mimeType === 'multipart/form-data' ) {
log.info('parsing multipart/form-data using multiparty');
var form = new multiparty.Form( _multipOptions );
form.parse(req, function(err, mpfields, mpfiles) {
if ( !err ) {
var bodyData = { fields: mpfields, files: mpfiles };
later.resolve(bodyData);
}
else {
later.reject(err);
}
});
}
else {
// Read the full message body before parsing (if available)
var bodyData : string = '';
req.on('data', function (data) { bodyData += data; });
req.on('end', function () {
if ( !bodyData || bodyData.length === 0 ) {
later.resolve({});
return later.promise;
}
log.info('Parsing "%s" as (%s)',bodyData,mimeType);
try {
switch(mimeType) {
case 'application/xml':
case 'text/xml':
xml2js.parseString(
bodyData,
{ explicitRoot: false, explicitArray: false },
function( err: Error, res:any ) {
if (err) {
_log.error('Error parsing XML data with ' );
_log.error( err );
later.reject(err);
}
else {
log.info('Parsed XML as: %s', JSON.stringify(res));
later.resolve(res);
}
});
break;
case 'application/x-www-form-urlencoded':
log.info('Parsing "%s" ', bodyData );
var parsedData = querystring.parse(bodyData);
log.info('Parsed "%s" ', JSON.stringify(parsedData));
later.resolve( parsedData );
break;
default:
later.resolve( JSON.parse(bodyData) );
break;
}
}
catch( err ) {
_log.error('Error parsing incoming data - %s - with %s',bodyData,contentType );
_log.error( err );
later.reject(err);
}
});
}
return later.promise;
}
示例2:
app.post('/attachments', (req, res) => {
const form = new multiparty.Form();
form.parse(req, (err, fields, files) => {
if (err) {
res.status(500).send(err);
return;
}
if (!fields.name || fields.name[0] !== 'foo') {
res.status(400).json({
errors: {
name: 'this field must be foo',
},
});
return;
}
if (!fields.bool0 || fields.bool0[0] !== 'false') {
res.status(400).json({
errors: {
bool0: 'this field must be false',
},
});
return;
}
if (!fields.bool1 || fields.bool1[0] !== 'true') {
res.status(400).json({
errors: {
bool0: 'this field must be false',
},
});
return;
}
if (fields.ignored0 !== undefined || fields.ignored1 !== undefined) {
res.status(400).json({
errors: {
ignored0: 'this field must be undefined',
ignored1: 'this field must be undefined',
},
});
return;
}
const postedArray = fields['array[]'];
if (!postedArray || !Array.isArray(postedArray)) {
res.status(400).json({
errors: {
array: 'this field must be an array',
},
});
return;
}
if (postedArray.length !== 2 || postedArray[0] !== 'first!' || postedArray[1] !== 'first! E: missed it') {
res.status(400).json({
errors: {
array: 'invalid array contents',
},
});
return;
}
if (!fields.object || fields.object[0] !== '{"foo":1,"bar":0}') {
res.status(400).json({
errors: {
object: 'object must be converted to json',
},
});
return;
}
if (!files.text || files.text.length !== 1) {
res.status(400).json({
errors: {
text: 'this field is required',
},
});
}
const fileData = files.text[0];
if (fileData.originalFilename !== 'dummy.txt') {
res.status(400).json({
errors: {
text: 'this file must be named dummy.txt',
},
});
}
fs.readFile(fileData.path, (readErr, data) => {
if (readErr) {
res.status(500).send(readErr);
return;
}
if (!data.equals(expectedBuffer)) {
//.........这里部分代码省略.........
示例3: function
return function(req,res,next){
var isMulti = ack.reqres(req,res).req.isMultipart()
if(!isMulti)return next();
form.parse(req,function(err,fields,files){
if(err)return next(err)
req.body = req.body || {}
for(var x in fields){
req.body[x] = fields[x][0]
}
next()
})
}