冰火koa版上傳
一切以簡單粗暴易用的koa上傳文件、上傳圖片方式
使用指南
1、下載依賴庫(koa-icefire-upload)
yarn add koa-icefire-upload
2、在koa里使用
const {icefireUpload} = require('koa-icefire-upload'); router.post('/', async (ctx, next) => { let extendsions = ['png', 'jpeg', 'gif', 'jpg']; // 清一色小寫 允許的文件后綴 let filePath = path.join(__dirname, 'imgs/'); // 確保目錄存在,否則報錯, 文件上傳到此目錄 let params = await icefireUpload({ctx, extendsions, filePath}); ctx.body = params; // 這里就是上傳的信息 });
3、輸出案例
[ { "fieldname": "textfield", // 參數字段 "type": "text", // 參數類型, 普通參數text, "val": "111" //普通參數 }, { "fieldname": "filefield", // 參數字段 "type": "file", // 參數類型, 文件參數file, "errorMsg": "格式不規范" }, { "type": "file", // 參數類型, 文件參數file, "imgPath": "//Users/xxx/koa-icefire-upload/example/imgs//111.jpg", // 服務器地址 "imgKey": "111.jpg", // 文件名稱 "fieldname": "filefield" // 參數字段 } ]
4、 github地址:https://github.com/IceInTheFire/koa-icefire-upload