我正在尝试使用网络套接字上传文件。但是我收到错误“无法读取未定义的属性'发送'” 。
component.ts
ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";
ws_path = this.ws_scheme + '://' + window.location.host +
"/api/chat/stream/";
newWindow = null;
ws = new ReconnectingWebSocket(this.ws_path);
fileSelected($event){
let file = $event.target.files[0];
this.file = file;
if (file) {
let fileSize = 0;
if (file.size > 1024 * 1024)
this.fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB';
else
this.fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';
console.log(this.ws);
console.log(this.ws.url);
}
}
sendRequest() {
let blob =
(<HTMLInputElement>document.getElementById('fileToUpload')).files[0];
const BYTES_PER_CHUNK = 104857600; // 1MB chunk sizes.
const SIZE = blob.size;
let start = 0;
let end = BYTES_PER_CHUNK;
while( start < SIZE ) {
let chunk = blob.slice(start, end);
this.uploadFile(chunk);
start = end;
end = start + BYTES_PER_CHUNK;
}
this.openNewWindow();
}
executeAsync(func) {
setTimeout(func, 0);
}
uploadFile(blobFile) {
this.executeAsync(function() {
this.ws.send(blobFile);
console.log("after sending via websocket...")
});
}
我能够打开和关闭连接,但无法发送数据。错误发生在 this.ws.send(blobFile)中。该如何解决?