index.js:
const formidable = require('koa-formidable');//文件上傳
// 創(chuàng)建服務(wù)器
let app = new Koa();
// 處理文件及字符串
app.use(formidable({
// 設(shè)置上傳目錄,否則在用戶的temp目錄下
uploadDir:path.resolve('./public/files'),
// 默認根據(jù)文件算法生成hash字符串(文件名),無后綴
keepExtensions:true
}));
同時,頁面文件中:
<div class="form-group">
<label for="">歌曲文件</label>
<input type="file" name="file">
<p class="help-block">請上傳歌曲文件.</p>
</div>
//js中,獲取input
var inputs=document.querySelectorAll('input');
//傳文件,files原生屬性,jq不可以
fd.append('file',inputs[3].files[0]);
效果:

添加文件