js 遍历大文件

27 篇文章 1 订阅

js 遍历大文件

通过nodejs+socketio+vue-socket完成遍历大文件操作

服务端
const fs = require("fs");
const path = require("path");
const http = require("http");
const express = require("express");
const app = express();
const server = http.createServer(app);
const io = require("socket.io")(server);

io.on("connection", (socket) => {
  console.log("connected success");

  socket.on("getallfile", (data) => {
    console.log("接收到的数据:", data);
    // 获取全部文件
    /**
     * 文件遍历方法
     * @param filePath 需要遍历的文件路径
     */

    function fileDisplay(filePath, callback) {
      //根据文件路径读取文件,返回文件列表
      fs.readdir(filePath, function (err, files) {
        if (err) {
          console.warn(err);
        } else {
          //遍历读取到的文件列表
          files.forEach(function (filename) {
            //获取当前文件的绝对路径
            var filedir = path.join(filePath, filename);
            // console.log(filedir);
            //根据文件路径获取文件信息,返回一个fs.Stats对象
            fs.stat(filedir, function (eror, stats) {
              if (eror) {
                console.warn("获取文件stats失败");
              } else {
                var isFile = stats.isFile(); //是文件
                var isDir = stats.isDirectory(); //是文件夹
                if (isDir) {
                  fileDisplay(filedir, callback); //递归,如果是文件夹,就继续遍历该文件夹下面的文件
                } else {
                  callback(filedir);
                }
              }
            });
          });
        }
      });
    }

    //解析需要遍历的文件夹,我这以E盘根目录为例
    var filePath = path.resolve("\\\\Jxzy-netspace\\运销部");
    //调用文件遍历方法

    // var filedirs = [];
    var filedirs_maxLen = 0;
    fileDisplay(filePath, (data) => {
      // console.log(data);
      // filedirs.push(data);
      // console.log(filedirs);
      io.emit("output", data);
      // if (filedirs_maxLen < filedirs.length) {
      //   filedirs_maxLen = filedirs.length;
      // } else {
      //   console.log(filedirs);
      // }
    });
  });

  socket.on("disconnect", () => {
    console.log("disconnected");
  });
});
客户端
<template>
  <div class="home">
    <div v-for="(item, index) in filedirs" :key="index">
      <el-link class="link" @click="onClickLink(item)">{{ item }}</el-link>
    </div>
  </div>
</template>

<script>
import HomeAside from '../components/home/HomeAside.vue'
import HomeHeader from '../components/home/HomeHeader.vue'
import HomeMain from '../components/home/HomeMain.vue'
export default {
  name: 'Home',
  components: {
    HomeHeader,
    HomeAside,
    HomeMain,

  },
  data() {
    return {
      filedirs: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    };
  },
  created() {
    // this.getFileDir()
    this.$socket.emit("getallfile", "我是Vue端发送的数据")
  },
  mounted() {
    this.sockets.subscribe("output", (data) => {
      // console.log(data);
      this.filedirs.push(data)
    })
  },
  methods: {
    onClickLink(dir) {
      // console.log(dir);
      this.opendir(dir)
    },

    async opendir(dir) {
      const { data: res } = await this.$http.get("/opendir", {
        params: {
          dir
        }
      })
      console.log(res);
    }
  },
  sockets: {
    //查看socket是否渲染成功
    connect() {
      console.log("连接成功");
    },
    disconnect() {
      console.log("断开链接");
    },//检测socket断开链接 
    reconnect() {
      console.log("重新链接");
    },
  }
}
</script>

<style lang="scss" scoped>
.el-header,
.el-footer {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}

.home-container {
  height: 100vh;
}

.home-header {
  font-weight: 800;
}

.home-aside {
  box-sizing: border-box;
  width: 200px;
  padding: 20px;
  overflow-y: auto;
}

.home-main {
  // box-shadow: -3px 0px 5px -5px #8c8c8c; // 设置左侧阴影
  position: relative;
}

.box {
  width: 100%;
  height: 48%;
  margin: 1% 0px;
  overflow: hidden;
}
.left {
  // width: calc(30% - 10px);
  height: 100%;
  // background: #c9c9c9;
  float: left;
}
.resize {
  width: 5px;
  height: 100%;
  cursor: w-resize;
  float: left;
  background: linear-gradient(to right, #fff, rgb(218, 203, 203));
  opacity: 0.5;
  transition: all 0.5s;
}
.resize:hover {
  opacity: 1;
}
.mid {
  float: left;
  width: 70%;
  height: 100%;
  // background: #f3f3f3;
}

.link {
  font-size: 20px !important;
  margin-bottom: 12px;
}
</style>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码小余の博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值