Home > Web Front-end > JS Tutorial > body text

Use the node express framework to upload files (code example)

不言
Release: 2018-10-27 15:48:54
forward
1685 people have browsed it

本篇文章给大家带来的内容是关于使用node express框架实现文件的上传(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

本文主要采用node express框架实现,模板引擎用的是EJS

这是app.js文件

const express = require('express');
const cookieParser = require('cookie-parser');
const sessionParser = require('cookie-session');
const consolidate = require('consolidate');
const path = require('path');
const bodyParser = require('body-parser');
const multer = require('multer');
const fs = require('fs');
var app = express();
//配置模板引擎
app.set('view engine', 'html');
app.set('views', path.join(__dirname, 'views'));
app.engine('html', consolidate.ejs);
//解析cookie
app.use(cookieParser('sdadadasdasdasdas21312'));

//解析session
var keyArr = [];
for(var i = 0;i<100000;i++){
    keyArr.push(&#39;keys_&#39;+ Math.random());
}
app.use(sessionParser({name:&#39;index_id&#39;, keys:keyArr, maxAge:3600*60*20}))

//解析post数据
app.use(bodyParser.urlencoded({extended:false}));

//解析post文件
var objMulter = multer({dest:&#39;./public/upload&#39;})
app.use(objMulter.any());
//转发静态资源
app.use(&#39;/satic&#39;, express.static(path.join(&#39;public&#39;)));

//返回主页
app.get(&#39;/&#39;, (req, res) => {
    console.log(req.query, req.body, req.cookies, req.session)
    res.render(&#39;index&#39;,{name:&#39;index&#39;});
})
 /*   [ { fieldname: &#39;file&#39;,
    originalname: &#39;back.jpg&#39;,
    encoding: &#39;7bit&#39;,
    mimetype: &#39;image/jpeg&#39;,
    destination: &#39;./public/upload&#39;,
    filename: &#39;6c863b25d379a6f9d61e2495c2e03206&#39;,
    path: &#39;public\\upload\\6c863b25d379a6f9d61e2495c2e03206&#39;,
    size: 43300 } ] */
//上传文件
app.use(&#39;/file&#39;, (req, res) => {
    var oldName = req.files[0].path;
    var newName = oldName + path.parse(req.files[0].originalname).ext;
    //console.log("ext",path.parse(req.files[0].origial).ext)
    fs.rename(oldName, newName, (err) => {
        if(err){
            res.send(&#39;err&#39;);
        }else{
            res.send(&#39;success&#39;);
        }
    })
})
//监听端口
var server = app.listen(&#39;8000&#39;, function (req, res) { 
    var port = server.address().port;
    console.log(&#39;success in port:&#39; + port);
 })
Copy after login

这是form.html文件

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>首页</title>
    <style>
        .container{
         height: 400px;
         width: 400px;
         background-color: gainsboro;
     }
    </style>
</head>
<body>
    <span>这是<%=name%>页面</span>
    <form action="/file" method="POST" enctype="multipart/form-data">
        <div>
            <input type="file" name="file">
            <button type="submit" id="upload">上传</button>
        </div>
    </form>
</body>
</html>
Copy after login

The above is the detailed content of Use the node express framework to upload files (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template