當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。