依賴
創(chuàng)新互聯(lián)公司專注于東乃網(wǎng)站建設服務及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供東乃營銷型網(wǎng)站建設,東乃網(wǎng)站制作、東乃網(wǎng)頁設計、東乃網(wǎng)站官網(wǎng)定制、小程序定制開發(fā)服務,打造東乃網(wǎng)絡公司原創(chuàng)品牌,更為您提供東乃網(wǎng)站排名全網(wǎng)營銷落地服務。
formidable
代碼
前端
Node
'use strict'; const fs = require('fs'); const path = require('path'); const awaitWriteStream = require('await-stream-ready').write; const sendToWormhole = require('stream-wormhole'); const formidable = require("formidable"); const Controller = require('../../core/base_controller'); class FileController extends Controller { async parse(req) { const form = new formidable.IncomingForm(); return new Promise((resolve, reject) => { form.parse(req, (err, fields, files) => { resolve({ fields, files }) }) }); } /** * 上傳文件,兼容單文件和多文件 * @param customName 單文件自定義文件名 * @param isAjax 上傳方式 */ async upload() { const { ctx, logger } = this; const extraParams = await this.parse(ctx.req); let { multipleFile, customName, isAjax } = extraParams && extraParams['fields']; logger.info(multipleFile, customName, isAjax); if (isAjax === 'yes') { const urls = []; for (let key in extraParams.files) { const file = extraParams.files[key]; logger.info('file.name', file.name); logger.info('customName', customName); const stream = fs.createReadStream(file.path); const fileName = customName ? (customName + path.extname(file.name)) : file.name; const target = path.join(this.config.baseDir, 'app/public/upload', fileName); const writeStream = fs.createWriteStream(target); try { await awaitWriteStream(stream.pipe(writeStream)); } catch (err) { await sendToWormhole(stream); throw err; } urls.push(target); } this.success({ urls }) } } } module.exports = FileController;
總結
以上所述是小編給大家介紹的Egg.js 中 AJax 上傳文件獲取參數(shù),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!