Large file upload with WebSocket

后端 未结 6 2047
自闭症患者
自闭症患者 2020-11-30 00:24

I\'m trying to upload large files (at least 500MB, preferably up to a few GB) using the WebSocket API. The problem is that I can\'t figure out how to write \"send this slice

6条回答
  •  长情又很酷
    2020-11-30 00:28

    In order to serialize this operation you need the server to send you a signal every time a slice is received & written (or an error occurs), this way you could send the next slice in response to the onmessage event, pretty much like this:

    function Uploader(url, file) {
        var fs = new FileSlicer(file);
        var socket = new WebSocket(url);
    
        socket.onopen = function() {
           socket.send(fs.getNextSlice());
        }
        socket.onmessage = function(ms){
            if(ms.data=="ok"){
               fs.slices--;
               if(fs.slices>0) socket.send(fs.getNextSlice());
            }else{
               // handle the error code here.
            }
        }
    }
    

提交回复
热议问题