NodeJS使用formidable實現文件上傳
最近自學了一下NodeJS,然后做了一個小demo,實現歌曲的添加、修改、播放和刪除的功能,其中自然要實現音樂和圖片的上傳功能。于是上網查找資料,找到了一個formidable插件,該插件可以很好的實現文件的上傳功能。該小demo用到了MySQL數據庫,所有的數據都存放到了數據庫中。下面簡單說一些如何使用。
1.創(chuàng)建app.js主文件
const express = require('express');
const router = require('./router');
const path = require('path');
const bodyParser = require('body-parser');
const app = express();
//靜態(tài)資源服務
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
app.use('/node_modules', express.static(path.join(__dirname, 'node_modules')));
//配置模板引擎
app.set('views', path.join(__dirname, 'views'));
app.engine('.html', require('ejs').renderFile);
app.set('view engine', 'html');
//配置解析普通表單post請求體
app.use(bodyParser.urlencoded({extended:false}));
//加載路由系統(tǒng)
app.use(router);
app.listen(3000, '127.0.0.1', () => {
console.log('server is running at port 3000.');
})
2.html文件中的form表單
add.html文件:
<form action="/add" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="title">標題</label>
<input type="text" class="form-control" id="title" name="title" placeholder="請輸入音樂標題">
</div>
<div class="form-group">
<label for="artist">歌手</label>
<input type="text" class="form-control" id="singer" name="singer" placeholder="請輸入歌手名稱">
</div>
<div class="form-group">
<label for="music_file">音樂</label>
<input type="file" id="music" name="music" accept="audio/*">
<p class="help-block">請選擇要上傳的音樂文件.</p>
</div>
<div class="form-group">
<label for="image_file">海報</label>
<input type="file" id="poster" name="img" accept="image/*">
<p class="help-block">請選擇要上傳的音樂海報.</p>
</div>
<button type="submit" class="btn btn-success">點擊添加</button>
</form>
注意:method="post" enctype="multipart/form-data"
3.創(chuàng)建路由router.js文件
const express = require('express');
const router = express.Router();
const handler = require('./handler');
router
.get('/', handler.showIndex)
.get('/musicList', handler.getMusicList)
.get('/add', handler.showAdd)
.post('/add', handler.doAdd)
.get('/edit', handler.showEdit)
.post('/edit', handler.doEdit)
.get('/remove', handler.doRemove)
module.exports = router;
注意:router.js文件中的依賴不用多說。
4.創(chuàng)建handler.js文件
const formidable = require('formidable');
const config = require('./config');
const db = require('./common/db');
const path = require('path');
const fs = require('fs');
exports.doAdd = (req, res) => {
const form = new formidable.IncomingForm();
form.uploadDir = config.uploadDir;//上傳文件的保存路徑
form.keepExtensions = true;//保存擴展名
form.maxFieldsSize = 20 * 1024 * 1024;//上傳文件的最大大小
form.parse(req, (err, fields, files) => {
if (err) {
throw err;
}
const title = fields.title;
const singer = fields.singer;
const music = path.basename(files.music.path);
const img = path.basename(files.img.path);
db.query('INSERT INTO music (title,singer,music,img) VALUES (?,?,?,?)', [
title,
singer,
music,
img
], (err, rows) => {
if (err) {
throw err;
}
res.redirect('/');
})
})
};
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
如何使用nvm實現nodejs版本管理(版本刪除,版本切換及版本添加)
這篇文章主要給大家介紹了關于如何使用nvm實現nodejs版本管理(版本刪除,版本切換及版本添加)的相關資料,nvm是一個node版本管理工具,通過它可以安裝多種node版本并且可以快速、簡單的切換node版本,需要的朋友可以參考下2023-10-10
node.js中的querystring.parse方法使用說明
這篇文章主要介紹了node.js中的querystring.parse方法使用說明,本文介紹了querystring.parse的方法說明、語法、接收參數、使用實例和實現源碼,需要的朋友可以參考下2014-12-12
nodejs創(chuàng)建簡易web服務器與文件讀寫的實例
下面小編就為大家?guī)硪黄猲ode js系列課程-創(chuàng)建簡易web服務器與文件讀寫的實例。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
Node.js報錯信息Error:?Cannot?find?module?'XXX'問題及解
這篇文章主要介紹了Node.js報錯信息Error:?Cannot?find?module?'XXX'問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
Node.js開發(fā)之套接字(socket)編程入門示例
這篇文章主要介紹了Node.js開發(fā)之套接字(socket)編程,結合簡單實例形式分析了node.js套接字socket客戶端與服務器端相關實現與使用技巧,需要的朋友可以參考下2019-11-11

