zoukankan      html  css  js  c++  java
  • nodejs formidable混合表单提交

    废话不多说,直接上代码:

     前端页面:

    <!DOCTYPE html>
    <html>
    <head>
    <link rel=’stylesheet’ href=’/stylesheets/style.css’ />
    </head>
    <body>
    <h5>信息:<%= locals.title %></h5>
    <p>Welcome to <%= title %></p>

    <img src='./images/<%= locals.imginfo %>' width='200'/>

    <h2><%= locals.username %></h2>

    <form method="post" enctype="multipart/form-data" action="/file-upload">
    <input type="text" name="username">
    <input type="password" name="password">
    <input type="file" name="thumbnail">
    <input type="submit">
    </form>
    </body>
    </html>

    后端处理:

    //formidable
    router.get('/formidable', function(req, res, next) {
    res.render('formidable', {
    title: 'formidable'
    });
    });

    router.post('/file-upload', function(req, res, next) {
    console.log('开始文件上传....');
    var form = new formidable.IncomingForm();
    //设置编辑
    form.encoding = 'utf-8';
    //设置文件存储路径
    form.uploadDir = "./public/images/";
    //保留后缀
    form.keepExtensions = true;
    //设置单文件大小限制
    form.maxFieldsSize = 2 * 1024 * 1024;
    //form.maxFields = 1000; 设置所以文件的大小总和

    form.parse(req, function(err, fields, files) {
    //console.log(fields);
    // var originName=files.thumbnail.name;
    console.log(files.thumbnail.path);
    console.log('文件名:' + files.thumbnail.name);

    //随机文件名调用
    var t = (new Date()).getTime();
    //生成随机数
    var ran = parseInt(Math.random() * 8999 + 10000);
    //拿到扩展名
    var extname = path.extname(files.thumbnail.name);

    var username = fields.username;
    var password = fields.password;


    //文本测试
    console.log('name:' + username);
    console.log('pass:' + password);



    var oldpath = path.normalize(files.thumbnail.path);

    //新的路径
    //随机文件名调用
    // let newfilename=t+ran+extname;
    var newfilename = files.thumbnail.name;

    //插入数据库
    var img = new Img({
    username: username,
    newfilename: newfilename
    })
    img.save(function(err) {
    if(err) {
    console.log(err);
    //res.send(400);
    } else {
    console.log("信息提交成功!");
    res.render('formidable', { title: '文件上传成功:', imginfo: newfilename,name:username
    });
    }
    });

    var newpath = './public/images/' + newfilename;
    console.warn('oldpath:' + oldpath + ' newpath:' + newpath);
    fs.rename(oldpath, newpath, function(err) {
    if(err) {
    console.error("改名失败" + err);
    }

    //随机文件名调用
    //res.render('formidable', { title: '文件上传成功:', imginfo: newfilename,name:username});

    //原文件名
    //res.render('formidable', { title: '文件上传成功:', imginfo: originName,name:username});
    });

    //res.end(util.inspect({fields: fields, files: files}));
    });

    });

  • 相关阅读:
    7、NFC技术:让Android自动运行程序
    6、Android中的NFC技术
    5、NFC概述
    Delphi XE7中开发安卓程序一些有用的帮助资源
    Delphi开发安卓程序的感受
    Tomcat绿色版启动"startup.bat"一闪问题的解决方法!
    Delphi判断字符串中是否包含汉字,并返回汉字位置
    Delphi的DLL里如何实现定时器功能?
    Delphi的DLL里如何实现定时器功能?
    VS2013如何添加LIb库及头文件的步骤
  • 原文地址:https://www.cnblogs.com/agen-su/p/7771378.html
Copyright © 2011-2022 走看看