Node.js-文件上传

6. 文件上传

File.js: npm install uuid;npm install formidable;

const http = require('http');
const fs = require('fs');
const {URL} = require('url');
const path = require('path');
const querystring = require('querystring');
const formidable = require('formidable');//上传文件模块
const uuid = require('uuid/v1');//命名文件
// 创建服务
const server = http.createServer(function (req, res) {
    const url = new URL(req.url, 'http://' + req.headers.hostname);
    // 判断路径
    switch (url.pathname) {
        case '/uploadForm':
            fs.readFile('./file.html', function (err, data) {
                if (err) throw err;
                res.writeHead(200, {
                    'Content-type': 'text/html;charset=utf8'
                });
                res.end(data);
            });
            break;
        case '/upload':
            // 实例化
            const form = new formidable.IncomingForm({
                // 图片文件上传路径
                uploadDir: './file',
                keepExtensions:true,//显示文件后缀
                maxFileSize: 10 *1024 * 1024//文件大小
            });
            // 处理多选
            let fields = {likes:[]};
            form.on('field',function (name,value) {
                // console.log(name,value);
                if(name === 'likes'){
                    fields.likes.push(value)
                }else {
                    fields[name] = value;
                }
            });
            // 图片信息
            form.on('end',function () {
                console.log(fields);
            });
            // 对下面的error事件
            form.on('error',function (err) {
                res.writeHead(200,{
                    'Content-type': 'text/html;charset=utf8'
                });
               res.end('图片上传失败'+err.message);
            });
            // 解析请求体
            form.parse(req,function (err,fields,files) {
                if(err){
                    return;
                }
                // 所有字段信息
                // 修改文件名fs.renameSync(files.pic.path,path.join(form.uploadDir,`${uuid()}.${path.extname(files.pic.path)}`))
                res.writeHead(200,{
                    'Content-type': 'text/html;charset=utf8'
                });
                res.end('表单提交成功');
            });
            break;
        default:
            res.writeHead(404, {
                'Content-type': 'text/html;charset=utf8'
            });
            res.end('<h1>404 Not Found</h1>');
    }
});
server.listen(8086, function () {
    console.log('http server is running on port 8086');
});

File.html:主要enctype="multipart/form-data"

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登陆</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
    用户名:<input type="text" name="username"><br/>
    爱好:<input type="checkbox" name="likes" value="电影">电影
    <input type="checkbox" name="likes" value="读书">读书
    <input type="checkbox" name="likes" value="运动">运动<br/>
    头像:<input type="file" name="image"><br/>
    <button>注册</button>
</form>
</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值