<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. el-tree或者el-table里边的slot-scope传递变量但是没有元素对象解决方法

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

  2. 面试题30:包含min函数的栈

      思路: 1.首先将栈的基本结构写出 #初始化栈的写法 def __init__(self): self.stack = [] #栈的压入 (加self是实例化,如果前面加入静态装饰器啥的,就不需要 ...

  3. (转)SQL注入原理

    原文地址:http://www.cnblogs.com/rush/archive/2011/12/31/2309203.html SQL Injection:就是通过把SQL命令插入到Web表单递交或 ...

  4. 推荐系统之隐语义模型LFM

    LFM(latent factor model)隐语义模型,这也是在推荐系统中应用相当普遍的一种模型.那这种模型跟ItemCF或UserCF的不同在于: 对于UserCF,我们可以先计算和目标用户兴趣 ...

  5. go 学习之字符串

    字符串 字符串是不可变值类型,内部⽤用指针指向 UTF-8 字节数组.默认值是空字符串 "".用索引号访问某字节,如 s[i], i不能是负数.不能⽤用序号获取字节元素指针,&am ...

  6. HDFS学习笔记二

    文章来源于:https://blog.csdn.net/xuejingfu1/article/details/52554174 文件写入staging(分阶段进行) 一个客户端的创建文件的请求并不直接 ...

  7. Redis的备份与恢复

    备份 dump.rdb:RDB方式的备份文件 appendonly.aof:AOF方式的备份文件 rdb 备份处理 # 编辑redis.conf文件,找到如下参数,默认开启. save 900 1 s ...

  8. Ubuntu命令行操作

    一.文件/文件夹管理 ls 列出当前目录文件(不包括隐含文件) ls -a 列出当前目录文件(包括隐含文件) ls -l 列出当前目录下文件的详细信息 cd .. 回当前目录的上一级目录 cd - 回 ...

  9. 从零开始做一个Android自动化

    移动端自动化简单说就是,写好操作app的程序,运行起来,自动执行程序和测试用例,输出执行结果,结果正确,测试通过. 自动化可以方便地完成安装/卸载.启动/运行.UI适配等环节,节省时间: 同一测试脚本 ...

  10. C++在windows平台下不存在strptime函数,可以绕过该函数

    https://blog.csdn.net/u011077672/article/details/50524469?utm_source=blogxgwz1