const forms = require('formidable'); function requestbody(opts) { opts = opts || {}; ... opts.multipart = 'multipart' in opts ? opts.multipart : false; opts.formidable = 'formidable' in opts ? opts.formidable : {}; ... // @todo: next major version, opts.strict support should be removed if (opts.strict && opts.parsedMethods) { throw new Error('Cannot use strict and parsedMethods options at the same time.') } if ('strict' in opts) { console.warn('DEPRECATED: opts.strict has been deprecated in favor of opts.parsedMethods.') if (opts.strict) { opts.parsedMethods = ['POST', 'PUT', 'PATCH'] } else { opts.parsedMethods = ['POST', 'PUT', 'PATCH', 'GET', 'HEAD', 'DELETE'] } } opts.parsedMethods = 'parsedMethods' in opts ? opts.parsedMethods : ['POST', 'PUT', 'PATCH'] opts.parsedMethods = opts.parsedMethods.map(function (method) { return method.toUpperCase() }) return function (ctx, next) { var bodyPromise; // only parse the body on specifically chosen methods if (opts.parsedMethods.includes(ctx.method.toUpperCase())) { try { if (opts.json && ctx.is(jsonTypes)) { bodyPromise = buddy.json(ctx, { encoding: opts.encoding, limit: opts.jsonLimit, strict: opts.jsonStrict, returnRawBody: opts.includeUnparsed }); } else if (opts.multipart && ctx.is('multipart')) { bodyPromise = formy(ctx, opts.formidable); } } catch (parsingError) { if (typeof opts.onError === 'function') { opts.onError(parsingError, ctx); } else { throw parsingError; } } } bodyPromise = bodyPromise || Promise.resolve({}); /** * Check if multipart handling is enabled and that this is a multipart request * * @param {Object} ctx * @param {Object} opts * @return {Boolean} true if request is multipart and being treated as so * @api private */ function isMultiPart(ctx, opts) { return opts.multipart && ctx.is('multipart'); } /** * Donable formidable * * @param {Stream} ctx * @param {Object} opts * @return {Promise} * @api private */ function formy(ctx, opts) { return new Promise(function (resolve, reject) { var fields = {}; var files = {}; var form = new forms.IncomingForm(opts); form.on('end', function () { return resolve({ fields: fields, files: files }); }).on('error', function (err) { return reject(err); }).on('field', function (field, value) { if (fields[field]) { if (Array.isArray(fields[field])) { fields[field].push(value); } else { fields[field] = [fields[field], value]; } } else { fields[field] = value; } }).on('file', function (field, file) { if (files[field]) { if (Array.isArray(files[field])) { files[field].push(file); } else { files[field] = [files[field], file]; } } else { files[field] = file; } }); if (opts.onFileBegin) { form.on('fileBegin', opts.onFileBegin); } form.parse(ctx.req); }); }
代码中删除了影响有关文件上传的相关逻辑
首先 multipart 为 true 是开启文件上传的关键。
然后 formy 函数处理了 http 解析和保存的一系列过程,最终将 files 抛出进行统一处理。代码中依赖了 formidable 这个库,我们其实也可以直接使用这个库对文件进行处理。(上面的原生 node upload 只是简单地处理了一下)
opts.formidable 是 formidable 的 config 可以设置文件大小,保存的文件路径等等。
eggjs
使用 eggjs 进行文件上传需要现在配置文件中开启
config.multipart = { mode: "file", fileSize: "600mb" };
然后通过 ctx.request.files[0] 就能取到文件信息。
文件上传接口的转发