zoukankan      html  css  js  c++  java
  • Nodejs 文件上传

    node js 接收表单数据原理

    /**
     * node js 接收表单数据
     */
    const http = require("http");
    const qs   = require("querystring");
    
    http.createServer((request, response) => {
        // 表单提交的原理
        if (request.url === "/post" && request.method.toLowerCase() === "post") {
            // 1. 设置接收的变量
            let formData = "";
    
            // 2. 接收小段数据
            request.on("data", buf => {
                formData += buf;
            });
    
            // 3. 监听所有数据传递完毕事件
            request.once("end", () => {
                formData = qs.parse(formData);
                console.log(formData); // object
                console.log("数据接收完成");
            });
        }
    }).listen(3000);
    

    使用 formidable 上传文件

    /**
     * 使用formidable上传图片
     */
    const http = require("http");
    const fs   = require("fs");
    const formidable = require("formidable");
    const uuidv1 = require("uuid/v1");
    const path = require("path");
    
    http.createServer((request, response) => {
        if (request.url == "/") {
            fs.readFile(__dirname+"/index.html", (err, data)=>{
                if (err) {throw err;}
                response.setHeader("content-type", "text/html;charset=utf8;");
                response.end(data.toString());
            });
        }
    
        if (request.url === "/post" && request.method.toLowerCase() === "post") {
            // 1.实例化对象
            const form = new formidable();
            // 2.设置文件上传的路径, 默认就会自动上传到这个目录中,这个目录必须要存在,否则报错, 不建议使用相对路径
            form.uploadDir = __dirname + '/uploads';
    
    
            // 3.获取表单内容
            form.parse(request, (err, fields, files)=> {
                /****************** 利用formidable的文件名
                // 3.1 获取原文件名
                let ext     = path.extname(files.file.name);
                // 3.2 获取上传文件的路径
                let oldPath = files.file.path;
                // 3.3 拼接新路径
                let newPath = oldPath + ext;
                console.log(newPath);
                // 3.4 修改文件名
       
                或者使用 uuid 这个包, 二选一
                ************************************/
                // 3.1 获取独一无二的一个字符串 uuid 
                let uuid = uuidv1();
                // 3.2 获取上传文件的后缀
                let ext = path.extname(files.file.name);
                // 3.3 获取路径
                let oldPath = files.file.path;
                let newPath = __dirname + "/uploads/" + uuid + ext;
                // 3.4 修改文件名
                
                fs.rename(oldPath, newPath, err=>{
                    if (err) throw err;
                    response.end("文件上传成功");
                });
                response.end("images");
            });
        }
        // response.end("404");
    }).listen(3000);
    
  • 相关阅读:
    !!!最常用正则表达式语法
    RunMessageScript from spy
    已知进程、线程、窗体三者中某一个的句柄,需要查找另外两者的句柄。
    如何抓取一个当前运行软件所使用的内存
    谢谢你的伤害
    游摸底河有感
    九月无诗
    游石人公园有感
    影响35岁前成功的好习惯与坏习惯
    创业经验十二谈,愿有志者共勉(转)
  • 原文地址:https://www.cnblogs.com/liaohui5/p/10581624.html
Copyright © 2011-2022 走看看