<template>
  <div class="pad20">
    <input id="file" ref="file" type="file" />
    <button id="send" @click="send">发送</button>
    <div id="progress">{{progress}}</div>
    <div>{{(endTime-startTime)/1000}}s</div>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
export default {
  name: "HelloWorld",
  components: {},
  data() {
    return {
      progress: "",
      startTime: "",
      endTime: ""
    };
  },
  created() {
    var that = this;
  },
  mounted() {
    var that = this;
    that.websocket();
  },
  computed: {
    ...mapGetters(["device"])
  },
  methods: {
    ...mapActions(["ToggleDevice"]),
    send: function() {
      fileList = this.$refs.file.files;
      console.log(fileList);
      file = fileList[0];
      this.startTime = new Date().getTime();
      ws.send("begin");
    },
    websocket: function() {
      var that = this;
      window.ws = "";
      var paragraph = 1024 * 1024;
      //var paragraph = 1024;
      window.fileList = [];
      window.file = "";
      window.startSize = 0;
      window.endSize = 0;
      window.i = 0;
      window.j = 0;
      ws = new WebSocket(
        "ws://10.8.106.52:50000/platform/api/websocket/" +
          this.utils.localstorage_get("userInfo")["id"]
      );
      //连接成功建立后响应
      ws.onopen = function() {
        console.log("成功连接到");
        //ws.send(Math.random())
      };
      //收到服务器消息后响应
      var log = console.log;
      window.onmessFN = function(flag) {
        console.log("传输");
        that.endTime = new Date().getTime();
        if (endSize < file.size) {
          // console.log("file.size:" + file.size);
          startSize = endSize;
          endSize += paragraph;
          // console.log("startSize:" + startSize);
          // console.log("endSize:" + endSize);
          //console.log("file---haha----:" + file);
          that.progress =
            Math.round((startSize / file.size) * 10000) / 100.0 + "%";
          if (file.webkitSlice) {
            var blob = file.webkitSlice(startSize, endSize);
          } else if (file.mozSlice) {
            var blob = file.mozSlice(startSize, endSize);
          } else {
            var blob = file.slice(startSize, endSize);
          }
          var reader = new FileReader();
          reader.readAsArrayBuffer(blob);
          reader.onload = function loaded(evt) {
            var ArrayBuffer = evt.target.result;
            log("发送文件第" + i++ + "部分");
            //console.log(evt.target.result)
            ws.send(ArrayBuffer);
          };
        } else {
          ws.send("end");
          that.progress = "100%";
          // console.log("endSize >= file.size-->" + e.data + "<---");
          // console.log("endSize >= file.size-->endSize:" + endSize);
          console.log("endSize >= file.size-->file.size:" + file.size);
          startSize = endSize = 0;
          i = 0;
          log("发送" + file.name + "完毕");
          log("发送文件完毕");
          function get_filemd5sum(ofile) {
            var file = ofile;
            var tmp_md5;
            var blobSlice =
                File.prototype.slice ||
                File.prototype.mozSlice ||
                File.prototype.webkitSlice,
              // file = this.files[0],
              chunkSize = 8097152, // Read in chunks of 2MB
              chunks = Math.ceil(file.size / chunkSize),
              currentChunk = 0,
              spark = new SparkMD5.ArrayBuffer(),
              fileReader = new FileReader();
            fileReader.onload = function(e) {
              // console.log('read chunk nr', currentChunk + 1, 'of', chunks);
              spark.append(e.target.result); // Append array buffer
              currentChunk++;
              var md5_progress = Math.floor((currentChunk / chunks) * 100);
              if (currentChunk < chunks) {
                loadNext();
              } else {
                tmp_md5 = spark.end();
                console.log("#####", tmp_md5);
              }
            };
            fileReader.onerror = function() {
              console.warn("oops, something went wrong.");
            };
            function loadNext() {
              var start = currentChunk * chunkSize,
                end =
                  start + chunkSize >= file.size
                    ? file.size
                    : start + chunkSize;
              fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
            }
            loadNext();
          }
          console.log("$$$$", get_filemd5sum(file));
        }
      };
      ws.onmessage = function(e) {
        console.log("服务器说" + e.data);
        if (e.data == "ok") {
          window.onmessFN();
        }
      };
      //连接关闭后响应
      ws.onclose = function() {
        console.log("关闭连接");
        ws = null;
      };
    }
  }
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.ivu-layout-header {
  background: white;
}
.searchInput {
  width: 250px;
}
.searchCon {
  position: relative;
}
.pop {
  position: absolute;
  left: 0;
  bottom: -2px;
}
.popin {
  width: 250px;
}
.seletItem {
  cursor: pointer;
}
.cardCon {
  padding-left: 20px;
}
.wordline {
  font-size: 12px;
}
.barChart {
  width: 100%;
  height: 250px;
}
.formteshu table td {
  padding: 3px 7px;
  text-align: right;
}
.search {
  width: 100%;
  margin-bottom: 10px;
}
.search td {
  padding: 4px 5px;
}
</style>

websocket 文件上传的更多相关文章

  1. 基于 WebSocket 的聊天和大文件上传(有进度提示)完美实现

    大家好,好久没有写文章了,当然不是不想写,主要是工作太忙,公司有没有网络环境,不让上网,所以写的就少了.今天是2019年的最后一天,明天就要开始新的一年,当然也希望自己有一个新的开始.在2019年的最 ...

  2. Spring MVC 笔记 —— Spring MVC 文件上传

    文件上传 配置MultipartResolver <bean id="multipartResolver" class="org.springframework.w ...

  3. springMVC实现文件上传下载

    上传文件和下载文件是个常用的技能,在哪里开发几乎都能遇见,而所有的上传控件各不相同,插件很多,后台也有很多,这里我只尝试过这个方法觉的还够简洁.具体如下实现: 1.spring-mvc.xml配置   ...

  4. SpringCloud+Feign环境下文件上传与form-data同时存在的解决办法

    最近项目转型使用SpringCloud框架下的微服务架构,各微服务之间使用Feign进行调用.期间,发现若被调用方法涉及到文件上传且仅存在单个文件时,一切正常,代码片段如下: @RequestMapp ...

  5. webuploader 文件上传总结

    $(function() { /** * 初始化Web Uploader */ var uploader = WebUploader.create({ swf : '../mobile/Uploade ...

  6. SpringBoot文件上传异常之提示The temporary upload location xxx is not valid

    原文: 一灰灰Blog之Spring系列教程文件上传异常原理分析 SpringBoot搭建的应用,一直工作得好好的,突然发现上传文件失败,提示org.springframework.web.multi ...

  7. 服务器文档下载zip格式 SQL Server SQL分页查询 C#过滤html标签 EF 延时加载与死锁 在JS方法中返回多个值的三种方法(转载) IEnumerable,ICollection,IList接口问题 不吹不擂,你想要的Python面试都在这里了【315+道题】 基于mvc三层架构和ajax技术实现最简单的文件上传 事件管理

    服务器文档下载zip格式   刚好这次项目中遇到了这个东西,就来弄一下,挺简单的,但是前台调用的时候弄错了,浪费了大半天的时间,本人也是菜鸟一枚.开始吧.(MVC的) @using Rattan.Co ...

  8. HTML文件上传与下载

    文件下载 传统的文件下载有两种方法: 使用<a/>标签,href属性直接连接到服务器的文件路径 window.location.href="url" 这两种方法效果一样 ...

  9. springboot文件上传 流的方式 后台计算上传进度

    //代码 public static void main(String[] args) throws Exception { String path = "f:/svn/t_dictiona ...

随机推荐

  1. XenServer(服务器虚拟化平台)

    Citrix Xenserver,思杰基于Xen的虚拟化服务器.Citrix XenServer是一种全面而易于管理的服务器虚拟化平台,基于强大的 Xen Hypervisor 程序之上.Xen技术被 ...

  2. create-react-app 配置 修改

    1.端口号修改:https://www.jianshu.com/p/80a7603dda70(亲测有效)   在 根据 package.json 的启动,node_modules文件夹里面搜索reac ...

  3. Linux基本常用命令|ubuntu获取root权限

    我用的是ubuntu12.4系统,因为默认是没有获取root的权限的 下边讲解怎么获取root权限 在终端中输入: sudo passwd root Enter new UNIX password: ...

  4. /proc/interrupts /proc/stat 查看中断信息

    /proc/interrupts列出当前所以系统注册的中断,记录中断号,中断发生次数,中断设备名称 如下图:从左至右:中断号   中断次数  中断设备名称 从上图可知中断号为19的arch_timer ...

  5. 记C函数指针的“小坑”

    今天遇到一个C函数指针的小坑,索性记下来. 我在a.c 文件里面,引用b.c 文件的函数声明作为指针引用 比如在a.c生命一个函数指针 typedef void (*free)(void *val) ...

  6. POJ 1410 Intersection (计算几何)

    题目链接:POJ 1410 Description You are to write a program that has to decide whether a given line segment ...

  7. 数据结构C语言实现

    顺序表实现 typedef int Position; typedef struct LNode *List; struct LNode { ElementType Data[MAXSIZE]; Po ...

  8. JavaScript 模拟后台任务

    读书笔记,请勿转载,发布,产权不归我所有,归以前作者所有,我只是做读书笔记. /*! * Copyright 2015 Google Inc. All rights reserved. * * Lic ...

  9. Windows Server 2008 R2 官方简体中文免费企业版/标准版/数据中心版

    Windows Server 2008 R2是一款微软发布的Windows服务器操作系统,和之前发布的Windows Server 2008相比功能更为完善运行更为稳定,提升了系统管理弹性.虚拟化.网 ...

  10. ubuntu下MySQL的安装及远程连接配置(转)

    1.命令窗口中输入sudo apt-get install mysql-server mysql-client 即可(配置文件位置:/etc/mysql/my.cnf 启动文件位置:/etc/init ...