2015-07-27 10 views
6

Quando inserisco un modulo multistrato,KoaJS come ottenere file da dati di moduli multipart?

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

Getta:

Error: Unsupported content-type: multipart/form-data 
at 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 a thunk which parses form and json requests 
 
* depending on the Content-Type. 
 
* 
 
* Pass a node request or an object with `.req`, 
 
* such as a 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); 
 
    }; 
 
};

poi, ho cambiato il codice

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

a

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

nessun errore, ma non riesco a ottenere il request'data:

debug(this.request.files.file); 

risultato non è definito.

Sto usando KoaJs.

risposta

0

Ok. Prima di tutto il codice che stai postando è il codice sorgente da co-body quindi aggiungere multipart/form-data non significa forzare questo pacchetto a gestire i dati multiparte se non è stato creato per farlo.

Invece di utilizzare co-body è possibile utilizzare co-busboy che è stato creato per gestire multipart/form-data.

1

Per KOA 2, cercare di analizzare async-busboy richiesta corpo come co-busboy non gioca bene con asincrono basato promessa.

Esempio dalla documentazione:

import asyncBusboy from 'async-busboy'; 

// Koa 2 middleware 
async function(ctx, next) { 
    const {files, fields} = await asyncBusboy(ctx.req); 

    // Make some validation on the fields before upload to S3 
    if (checkFiles(fields)) { 
    files.map(uploadFilesToS3) 
    } else { 
    return 'error'; 
    } 
} 
+0

[async-busboy] (https://github.com/m4nuC/async-busboy) ha un [problema che genera ReadStreams] (https://github.com/m4nuC/async-busboy/issues/1), quindi non è affidabile per il caricamento di più file ancora. –

+0

@manakor Questo problema è stato corretto – silkAdmin

0

Prova koa-body biblioteca.

Esempio:

Mettere questo middleware PRIMA del middleware percorso:

app.use(require('koa-body')({ 
    formidable:{ 
     uploadDir: __dirname + '/public/uploads', // directory where files will be uploaded 
     keepExtensions: true // keep file extension on upload 
    }, 
    multipart: true, 
    urlencoded: true, 
})); 

Quindi utilizzare nel percorso middleware

async function(ctx, next) { 
    ctx.request.body.files.file // file is the input name 
} 

Questo codice è per KoaJs 2, ma questa biblioteca funziona con KoaJs 1 anche.

Problemi correlati