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

    方法一:---------支持中文,图片显示------------
    //-------文件上传------
    router.post('/imgupload',function(req,res){
    //-----------------------------------------
    var form = new multiparty.Form();
    //设置编辑
    form.encoding = 'utf-8';
    //设置文件存储路径
    form.uploadDir = "./uploads/";
    //设置单文件大小限制
    form.maxFilesSize = 2 * 1024 * 1024;
    //form.maxFields = 1000; 设置所以文件的大小总和

    form.parse(req, function(err, fields, files) {

    for(var key in files){
    console.log(key+'=='+files[key]);
    for(var kk in files[key][0].headers){
    console.log(kk+'==='+files[key][0].headers[kk]);
    }
    }
    file1 = files['myimg'];
    paraname = file1[0].fieldName; //参数名myimg
    originalFilename = file1[0].originalFilename; //原始文件名
    tmpPath = file1[0].path;//uploadsmrecQCv2cGlZbj-UMjNyw_Bz.txt
    fileSize = file1[0].size; //文件大小

    var timestamp=new Date().getTime(); //获取当前时间戳
    newPath= './public/images/'+timestamp+originalFilename;

    var fileReadStream = fs.createReadStream(tmpPath);
    var fileWriteStream = fs.createWriteStream(newPath);
    fileReadStream.pipe(fileWriteStream); //管道流
    fileWriteStream.on('close',function(){
    console.log('copy over');
    });
    /*
    function writeFile(data){
    fs.writeFile(newPath,data,function(error){
    if(error){
    throw error;
    }else{
    console.log("文件已保存");
    }
    });
    }
    fs.readFile(tmpPath,'ascii',function(err, data) {
    if (err) {
    console.log("读取失败");
    } else {
    writeFile(data);
    }
    });
    */

    //重命名为真实文件名
    /*
    fs.rename(tmpPath, newPath, function(err) {
    if(err){
    console.log('rename error: ' + err);
    } else {
    console.log('rename ok');
    }
    });
    */
    //删除临时文件夹中的文件
    //fs.unlinkSync(tmpPath);

    });
    //-----------------------------------------
    res.send('上传完成');
    });

    方法二:
    //----------有中文问题,图片显示不出来-----------
    //-------文件上传------
    router.post('/imgupload',function(req,res){
    //-----------------------------------------
    var form = new multiparty.Form();
    //设置编辑
    form.encoding = 'utf-8';
    //设置文件存储路径
    form.uploadDir = "./uploads/";
    //设置单文件大小限制
    form.maxFilesSize = 2 * 1024 * 1024;
    //form.maxFields = 1000; 设置所以文件的大小总和

    form.parse(req, function(err, fields, files) {

    for(var key in files){
    console.log(key+'=='+files[key]);
    for(var kk in files[key][0].headers){
    console.log(kk+'==='+files[key][0].headers[kk]);
    }
    }
    file1 = files['myimg'];
    paraname = file1[0].fieldName; //参数名myimg
    originalFilename = file1[0].originalFilename; //原始文件名
    tmpPath = file1[0].path;//uploadsmrecQCv2cGlZbj-UMjNyw_Bz.txt
    fileSize = file1[0].size; //文件大小

    var timestamp=new Date().getTime(); //获取当前时间戳
    newPath= './public/images/'+timestamp+originalFilename;

    function writeFile(data){
    fs.writeFile(newPath,data,function(error){
    if(error){
    throw error;
    }else{
    console.log("文件已保存");
    }
    });
    }
    fs.readFile(tmpPath,'ascii',function(err, data) {
    if (err) {
    console.log("读取失败");
    } else {
    writeFile(data);
    }
    });

    //重命名为真实文件名
    /*
    fs.rename(tmpPath, newPath, function(err) {
    if(err){
    console.log('rename error: ' + err);
    } else {
    console.log('rename ok');
    }
    });
    */
    //删除临时文件夹中的文件
    //fs.unlinkSync(tmpPath);

    });
    //-----------------------------------------
    res.send('上传完成');
    });

  • 相关阅读:
    Truck History POJ
    Constructing Roads POJ
    Building a Space Station POJ
    Networking POJ
    Jungle Roads POJ
    How Many Answers Are Wrong HDU
    harbor磁盘爆满,执行垃圾回收清理镜像
    阿里云服务器安装mysql镜像
    jenkins Publish over SSH 的配置与使用
    npm install报错 SyntaxError: Unexpected end of JSON input while parsing near '...=GmVg -----END PGP'
  • 原文地址:https://www.cnblogs.com/yu-hailong/p/7439871.html
Copyright © 2011-2022 走看看