文件上傳
multer模塊官網(wǎng)
- html頁面
<form method="post" enctype="multipart/form-data" class="uploadFile" action="lalocal-sales/customers/excel">
<button class="btn btn-success btn-file-up"><input type="file" name="file" class="file_up" id='file'>上傳文件</button>
</form>
- js代碼
$('.file_up').change(function() {
$('.uploadFile').submit();
});
- node層
先引入multer模塊谈息,設(shè)置文件上傳目錄
var multer = require('multer');
var upload = multer({
dest: './upload/'
});
再對(duì)上傳的文件進(jìn)行格式化
router.post("/customers/excel",upload.single('file'),function(req,res,next) {
var url = global.baseURL+req.url;
console.log('url................................'+url);
console.log('請(qǐng)求路徑.....'+req.url);
var obj = req.file;
var tmp_path = obj.path;
var new_path = "upload/excelfile.XLSX";
console.log("原路徑:" + tmp_path);
/*修改上傳文件地址*/
fs.rename(tmp_path, new_path, function(err) {
if (err) {
throw err;
}
});
最后上傳到服務(wù)器
superagent
.post(url)
.attach('file', new_path)
.end(function(err1, res1) {
if (res1.ok) {
console.log(JSON.stringify(res1.body));
fs.unlinkSync(new_path);//刪除
res.redirect(global.rootUrl+'custom');
}
});