Untitled

mail@pastecode.io avatar
unknown
javascript
a year ago
1.7 kB
1
Indexable
Never
function downloadBook(_event) {
        var socket = new WebSocket("ws://192.168.3.95:12345/transfer");
        var receivedBlobs = [];

        socket.onopen = function(event) {
          console.log("WebSocket opened.");
        };

        socket.onmessage = function(event) {
          console.log("Message received: " + event.data);
          receivedBlobs.push(event.data);

          if (event.data === "File transfer started.") {
            // File transfer has started, do something
          } else if (event.data === "File transfer complete.") {
            // File transfer is complete, combine the received blobs into a single Blob
                var combinedBlob = new Blob(receivedBlobs, { type: "application/octet-stream" });

                // Read the contents of the combined Blob using a FileReader
                var reader = new FileReader();
                reader.onloadend = function() {
                  var combinedBytes = new Uint8Array(reader.result);

                  // Save the combined bytes as a file
                  var blob = new Blob([combinedBytes], { type: "application/octet-stream" });
                  var url = URL.createObjectURL(blob);

                  var link = document.createElement("a");
                  link.href = url;
                  link.download = "video123.mp4";
                  document.body.appendChild(link);
                  link.click();
                };
                reader.readAsArrayBuffer(combinedBlob);
          }
        };

        socket.onclose = function(event) {
          console.log("WebSocket closed.");
        };
	}