socket获取web服务器文件上传,websocket 文件上传(示例代码)

发送

{{progress}}
{{(endTime-startTime)/1000}}s

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 

// 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 

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;

};

}

}

};

.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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值