node.js - KoaJS how to get files from multipart form data? -


when post multipart form,

<form name="acount_manage"  action="/update" enctype="multipart/form-data" method="post">     <input type="file" name="file"> </form> 

it throws:

error: unsupported content-type: multipart/form-data @ object.<anonymous> (e:\...\node_modules\co-body\lib\any.js:51:15) 

any.js:

/**   * module dependencies.   */    var json = require('./json');  var form = require('./form');  var text = require('./text');    var json_content_types = [    'application/json',    'application/json-patch+json',    'application/vnd.api+json',    'application/csp-report',    'application/ld+json'    ];    /**   * return a thunk parses form , json requests   * depending on content-type.   *   * pass node request or object `.req`,   * such koa context.   *   * @param {request} req   * @param {options} [opts]   * @return {function}   * @api public   */    module.exports = function(req, opts){    req = req.req || req;      // parse content-type    var type = req.headers['content-type'] || '';    type = type.split(';')[0];      // json    if (~json_content_types.indexof(type)) return json(req, opts);      // form    if ('application/x-www-form-urlencoded' == type) return form(req, opts);      // text    if ('text/plain' == type) return text(req, opts);      // invalid    return function(done){      var message = type ? 'unsupported content-type: ' + type : 'missing content-type';      var err = new error(message);      err.status = 415;      done(err);    };  };

then,i changed code

if ('application/x-www-form-urlencoded' == type) return form(req, opts); 

to

if ('application/x-www-form-urlencoded' == type || 'multipart/form-data'==type) return form(req, opts); 

no error ,but can't request'data :

debug(this.request.files.file); 

result undefined.

i using koajs.

for koa 2, try async-busboy parse request body co-busboy doesn't play promise based async.

example docs:

import asyncbusboy 'async-busboy';  // koa 2 middleware async function(ctx, next) {   const {files, fields} = await asyncbusboy(ctx.req);    // make validation on fields before upload s3   if ( checkfiles(fields) ) {     files.map(uploadfilestos3)   } else {     return 'error';   } } 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -