当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript multiparty.Form类代码示例

本文整理汇总了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;
}
开发者ID:micurs,项目名称:relaxjs,代码行数:67,代码来源:internals.ts

示例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)) {
//.........这里部分代码省略.........
开发者ID:Jyrno42,项目名称:tg-resources,代码行数:101,代码来源:test-server.ts

示例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()
    })
  }
开发者ID:AckerApple,项目名称:ack-node,代码行数:16,代码来源:router.ts


注:本文中的multiparty.Form类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。