本文介绍了使用WebSocket上传大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用WebSocket API上传大文件(至少500MB,最好高达几GB)。问题是我无法弄清楚如何编写发送此文件的片段,释放使用的资源然后重复。我希望我可以避免使用像Flash / Silverlight这样的东西。



目前,我正在使用以下内容:

 函数FileSlicer(文件){
//随机选取1MB切片,
//我不认为这个大小对于这个实验
this.sliceSize = 1024 * 1024;
this.slices = Math.ceil(file.size / this.sliceSize);

this.currentSlice = 0;

this.getNextSlice = function(){
var start = this.currentSlice * this.sliceSize;
var end = Math.min((this.currentSlice + 1)* this.sliceSize,file.size);
++ this.currentSlice;

返回file.slice(开始,结束);
}
}

然后,我会上传使用:

 函数Uploader(url,file){
var fs = new FileSlicer(file);
var socket = new WebSocket(url);

socket.onopen = function(){
for(var i = 0; i< fs.slices; ++ i){
socket.send(fs.getNextSlice) ()); //见下面
}
}
}

基本上这个立即返回,bufferedAmount不变(0),并且在尝试发送它之前,它会一直迭代并将所有切片添加到队列中;没有socket.afterSend允许我正确排队,这是我卡住的地方。

解析方案

:网络世界,浏览器,防火墙,代理服务器,因为这个答案已经改变了很多。现在,使用websockets
发送文件可以高效地完成,尤其是在局域网上。

Websockets对于双向通信非常有效,特别是当你有兴趣从服务器推送信息(最好是小)。它们充当双向套接字(因此它们的名称)。



Websockets似乎不适合在这种情况下使用。特别是考虑到使用它们会增加与某些代理,浏览器(IE)甚至防火墙的不兼容性。另一方面,上传文件只是发送POST请求给服务器与正文中的文件。浏览器非常擅长,大文件的开销几乎没有。不要使用websockets执行该任务。


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 of the file, release the resources used then repeat". I was hoping I could avoid using something like Flash/Silverlight for this.

Currently, I'm working with something along the lines of:

function FileSlicer(file) {
    // randomly picked 1MB slices,
    // I don't think this size is important for this experiment
    this.sliceSize = 1024*1024;
    this.slices = Math.ceil(file.size / this.sliceSize);

    this.currentSlice = 0;

    this.getNextSlice = function() {
        var start = this.currentSlice * this.sliceSize;
        var end = Math.min((this.currentSlice+1) * this.sliceSize, file.size);
        ++this.currentSlice;

        return file.slice(start, end);
    }
}

Then, I would upload using:

function Uploader(url, file) {
    var fs = new FileSlicer(file);
    var socket = new WebSocket(url);

    socket.onopen = function() {
        for(var i = 0; i < fs.slices; ++i) {
            socket.send(fs.getNextSlice()); // see below
        }
    }
}

Basically this returns immediately, bufferedAmount is unchanged (0) and it keeps iterating and adding all the slices to the queue before attempting to send it; there's no socket.afterSend to allow me to queue it properly, which is where I'm stuck.

解决方案

EDIT : The web world, browsers, firewalls, proxies, changed a lot since this answer was made. Right now, sending files using websocketscan be done efficiently, especially on local area networks.

Websockets are very efficient for bidirectional communication, especially when you're interested in pushing information (preferably small) from the server. They act as bidirectional sockets (hence their name).

Websockets don't look like the right technology to use in this situation. Especially given that using them adds incompatibilities with some proxies, browsers (IE) or even firewalls.

On the other end, uploading a file is simply sending a POST request to a server with the file in the body. Browsers are very good at that and the overhead for a big file is really near nothing. Don't use websockets for that task.

这篇关于使用WebSocket上传大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 05:31