<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. MySqL rownum 序号 类似于 oracle的rownum

    mysql中没有 rownum 序号的功能,所以需要自己去实现序号的功能. @rownum 只是一个变量 可以换为 @i 等其他变量,但必须有@符号 SELECT @rownum:=@rownum+1 ...

  2. el-tree或者el-table里边的slot-scope传递变量但是没有元素对象解决方法

    传统的是点击一个元素可以通过e.target进行获取,但是对于这种的回调函数里边加个e再进行e.target就获取不到元素了,更获取不到点击的位置那么可以在触发的地方写成箭头函数并且传递一个变量.如下 ...

  3. BZOJ 4484: [Jsoi2015]最小表示(拓扑排序+bitset)

    传送门 解题思路 \(bitset\)维护连通性,给每个点开个\(bitset\),第\(i\)位为\(1\)则表示与第\(i\)位联通.算答案时显然要枚举每条边,而枚举边的顺序需要贪心,一个点先到达 ...

  4. 专为渗透测试人员设计的 Python 工具大合集

    如果你对漏洞挖掘.逆向工程分析或渗透测试感兴趣的话,我第一个要推荐给你的就是Python编程语言.Python不仅语法简单上手容易,而且它还有大量功能强大的库和程序可供我们使用.在这篇文章中,我们会给 ...

  5. (三)修改内核大小,适配目标板Nand flash分区配置

    一. 修改内核大小 1. 在你的配置文件下uboot/include/config/xxx.h 里面有一个宏定义 #define MTDPARTS_DEFAULT "mtdparts=jz2 ...

  6. Appium移动端自动化:Appium-Desktp的使用以及定位元素方式总结

    一.appium-desktop功能介绍 1.打开appium-desktop,点击start session 2.打开后,点击屏幕右上角的搜索按钮 3.然后会打开配置页面,在本地服务配置信息同上面写 ...

  7. 前端模块化开发的价值(seaJs)

    随着互联网的飞速发展,前端开发越来越复杂.本文将从实际项目中遇到的问题出发,讲述模块化能解决哪些问题,以及如何使用 Sea.js 进行前端的模块化开发. 恼人的命名冲突 我们从一个简单的习惯出发.我做 ...

  8. tensorflow队列tf.FIFOQueue | enqueue | enqueue_many | dequeue | dequeue_many

    关于队列的相关知识,盗用一张https://blog.csdn.net/HowardWood/article/details/79406891的动态图 import tensorflow as tf ...

  9. CSS深入理解line-height

    1.line-height高度基于基线 2. 3.p元素的高度由行高决定的 4. 5.

  10. flink批处理中的source以及sink介绍

    一.flink在批处理中常见的source flink在批处理中常见的source主要有两大类: 1.基于本地集合的source(Collection-based-source) 2.基于文件的sou ...