• node


    html

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 
     4 <head>
     5     <meta charset="UTF-8">
     6     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     7     <meta http-equiv="X-UA-Compatible" content="ie=edge">
     8     <title>Document</title>
     9 </head>
    10 
    11 <body>
    12     <form action="http://127.0.0.1/dopost" enctype="multipart/form-data" method="POST">
    13         姓名:<input type="text" name="name"> 性别:
    14         <input type="radio" name="gender" value="男"><input type="radio" name="gender" value="女">女 年龄:
    15         <input type="number" name="age"> 图片:
    16         <input type="file" name="img">
    17         <input type="submit" value="提交">
    18     </form>
    19 </body>
    20 
    21 </html>

    js

     1 const http = require('http');
     2 const formidable = require('formidable');
     3 const util = require('util');
     4 const fs = require('fs');
     5 const path = require('path');
     6 
     7 
     8 //创建服务器
     9 const server = http.createServer(function (req, res) {
    10     if (req.url == '/dopost' && req.method.toLowerCase() == 'post') {
    11         // 插件- 请搜索:https://www.npmjs.com/package/formidable
    12         let form = new formidable.IncomingForm();
    13 
    14         //设置文件上传存放地址
    15         form.uploadDir = './uploads';
    16         // 设置文件上传大小(默认20M)
    17         form.maxFileSize = 200 * 1024 * 1024;
    18         //执行里面的回调函数的时候,表单已经全部接收完毕了。
    19         form.parse(req, function (err, fields, files) {
    20             // 时间戳
    21             let timestamps = Math.round(new Date().getTime() / 1000).toString();
    22             // 扩展名
    23             let extname = path.extname(files.img.name);
    24             // 旧路径
    25             let oldPath = __dirname + '/' + files.img.path;
    26             // 老文件名
    27             let oldFileName = files.img.name;
    28             // 新路径(默认无扩展名,需要引入path里面的得到extname得到)
    29             let newPath = __dirname + '/uploads/' + getDate() + timestamps + extname;
    30             // 新文件名
    31             let newFileName = getDate() + random + extname;
    32 
    33             //改名api - http://nodejs.cn/api/fs.html#fs_fs_rename_oldpath_newpath_callback
    34             fs.rename(oldPath, newPath, function (err) {
    35                 if (err) throw Error('修改文件名失败!~');
    36                 console.log('修改前的文件名:', oldFileName, '修改后的文件名:', newFileName);
    37                 res.writeHead(200, { 'content-type': 'text/plain' });
    38                 res.end('成功!~');
    39             });
    40         });
    41     }
    42 });
    43 
    44 
    45 // 日期方法.
    46 function getDate() {
    47     let date = new Date();
    48     let year = date.getFullYear();
    49     let month = date.getMonth() + 1;
    50     let day = date.getDate();
    51     return year + '-' + month + '-' + day + '-'
    52 }
    53 
    54 server.listen(80, '127.0.0.1');
  • 相关阅读:
    关于const_cast
    TinyXPath与TinyXML
    C++的异常
    如何让对象只能处于堆中
    股票的委托价、成交价与成本价的关系
    static_cast dynamic_cast reinterpret_cast const_cast
    C++ MD5,SHA1源码下载和简单调用
    C++中char[]转string
    HttpWorker
    winnet winhttp
  • 原文地址:https://www.cnblogs.com/cisum/p/9431259.html
Copyright © 2020-2023  润新知