成熟丰满熟妇高潮XXXXX,人妻无码AV中文系列久久兔费 ,国产精品一国产精品,国精品午夜福利视频不卡麻豆

您好,歡迎來到九壹網(wǎng)。
搜索
您的當(dāng)前位置:首頁NodeJS使用formidable實現(xiàn)文件上傳

NodeJS使用formidable實現(xiàn)文件上傳

來源:九壹網(wǎng)

最近自學(xué)了一下NodeJS,然后做了一個小demo,實現(xiàn)歌曲的添加、修改、播放和刪除的功能,其中自然要實現(xiàn)音樂和圖片的上傳功能。于是上網(wǎng)查找資料,找到了一個formidable插件,該插件可以很好的實現(xiàn)文件的上傳功能。該小demo用到了MySQL數(shù)據(jù)庫,所有的數(shù)據(jù)都存放到了數(shù)據(jù)庫中。下面簡單說一些如何使用。

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)資源服務(wù)
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">標(biāo)題</label>
 <input type="text" class="form-control" id="title" name="title" placeholder="請輸入音樂標(biāo)題">
 </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;//保存擴(kuò)展名
 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('/');
 })
 })
};

Copyright ? 2019- 91gzw.com 版權(quán)所有 湘ICP備2023023988號-2

違法及侵權(quán)請聯(lián)系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市萬商天勤律師事務(wù)所王興未律師提供法律服務(wù)