本文整理匯總了TypeScript中multiparty.Form.parse方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Form.parse方法的具體用法?TypeScript Form.parse怎麽用?TypeScript Form.parse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類multiparty.Form
的用法示例。
在下文中一共展示了Form.parse方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: 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()
})
}
示例2: 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;
}
示例3:
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)) {
//.........這裏部分代碼省略.........