谁能合理解释为什么网络服务器必须协助创建上传进度条?

发布于 2024-07-22 18:44:44 字数 421 浏览 11 评论 0原文

我一直在 Rails 中构建一个小型个人网站,并且已经达到了我需要能够上传文件的程度。 这几乎是一项微不足道的任务,根本不需要时间。

需要时间并慢慢侵蚀我的理智的是为用户提供上传进度条的任务。 我现在已经看到了至少十几种解决方案,它们似乎都有一个共同点——它们需要在 Web 服务器上安装一个模块。

鉴于我无法控制我的服务器,而且托管公司似乎不太可能安装任何此类动物,我陷入了困境。 真正令人抓狂的是,让服务器以任何方式、形式或形式参与到这个过程中是绝对没有必要的。

想一想:您的浏览器打开到远程服务器的套接字并开始发送数据。 您的浏览器确切地知道要发送多少字节,并且由于 TCP 确认的魔力,还知道有多少字节已到达服务器端。 那么,为什么以飞天面条怪物之名,没有简单的方法可以将这些数据呈现给 Javascript,而不用在该死的服务器上乱搞呢?

I've been putting together a small personal website in rails and have gotten to the point where I need to be able to upload files. This is an almost trivial task and took no time at all.

What is taking time - and is slowly sapping my sanity - is the task of providing the user with an upload progress bar. I've seen at least a dozen solutions now and they all seem to have one thing in common - they require the installation of a module on the web server.

Given that I have no control over my server that and it seems unlikely that the hosting company will ever undertake to install any such animal, I'm in a bit of a bind. What makes this truly maddening is that involving the server in this process in any way, shape, or form, is absolutely unnecessary.

Think about it: Your browser opens a socket to the remote server and starts sending data. Your browser knows exactly how many bytes are to be sent, and, thanks to the magic of TCP acknowledgments, is also aware of how many bytes have arrived at the server side. So why in the Name of The Flying Spaghetti Monster is there no simple way to present this data to Javascript without futzing around with the blasted server?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只怪假的太真实 2024-07-29 18:44:45

这里真正的限制是 HTML 表单。 它们是十多年前发明的,当时上传更大的文件似乎完全不可行。 因此,它们只为您提供一个回调:整个上传完成时(ACTION 参数)。

幸运的是,有一些免费的基于 Flash 的上传器提供进度条。 不用担心,大多数都会正常降级(如果用户没有闪存,则提供一个简单的文件上传字段)。 所有选项都可以通过 Javascript 进行控制。 而且,它们不需要任何特殊的服务器端基础设施。

检查 swfupload.org

The true limitation here are HTML Forms. They were invented over 10 years ago when the upload of larger files seemed completely unfeasible. So they provide you only with one callback: when the entire upload is done (ACTION parameter).

Luckily, there are some free flash based uploaders out there that offer progress bars. Don't worry, most of them will degrade gracefully (providing a simple file upload field if the user has no flash). All options can be controlled via Javascript. And, they don't require any special server-side infrastructure.

Check swfupload.org

小红帽 2024-07-29 18:44:45

现在除了 IE(所有版本)之外,几乎所有网络浏览器都可以实现这一点。

JavaScript 相当简单。 检查 sourceforge.net 上的 webfolder 项目。 它显示所有支持 HTML5 的浏览器的上传百分比。

 function upload() {
   document.folder.encoding = "multipart/form-data";
   if (document.folder.file.files) { 
       var xhr = new XMLHttpRequest();
        var fd = new FormData(document.folder);
        /* event listeners */
        xhr.upload.addEventListener("progress", uploadProgress, false);
        xhr.addEventListener("load", uploadComplete, false); // loadend if doesn't matter success or not 
        xhr.addEventListener("error", uploadError, false);
        xhr.open("POST", document.folder.action, true);
        fd.append('submit', 'Upload'); // keep consistent with view, so maybe return method in view
        fd.append('background', '1');
        xhr.send(fd); 
        return false;
   }
   return true;
}


function uploadProgress(evt) {
    if (evt.lengthComputable) {
        var per = (evt.loaded*100/evt.total).toFixed(0)+'%';
        getElement('_progress').innerHTML = per;
        document.title = 'Upload ('+per+')';
    }   else {
        getElement('_progress').innerHTML = evt.loaded;
    }
}

function uploadComplete(evt) {
    getElement('_progress').innerHTML = '100%';
    window.location.reload();
}

function uploadError(evt) {
    getElement('_progress').innerHTML = 'error';
}

我还有为具有更多娱乐能力的网站显示进度条的代码。

It is possible with mostly every web browser now except IE (all versions).

JavaScript is fairly simple. Check webfolder project at sourceforge.net. It shows upload % for all browsers supporting HTML5.

 function upload() {
   document.folder.encoding = "multipart/form-data";
   if (document.folder.file.files) { 
       var xhr = new XMLHttpRequest();
        var fd = new FormData(document.folder);
        /* event listeners */
        xhr.upload.addEventListener("progress", uploadProgress, false);
        xhr.addEventListener("load", uploadComplete, false); // loadend if doesn't matter success or not 
        xhr.addEventListener("error", uploadError, false);
        xhr.open("POST", document.folder.action, true);
        fd.append('submit', 'Upload'); // keep consistent with view, so maybe return method in view
        fd.append('background', '1');
        xhr.send(fd); 
        return false;
   }
   return true;
}


function uploadProgress(evt) {
    if (evt.lengthComputable) {
        var per = (evt.loaded*100/evt.total).toFixed(0)+'%';
        getElement('_progress').innerHTML = per;
        document.title = 'Upload ('+per+')';
    }   else {
        getElement('_progress').innerHTML = evt.loaded;
    }
}

function uploadComplete(evt) {
    getElement('_progress').innerHTML = '100%';
    window.location.reload();
}

function uploadError(evt) {
    getElement('_progress').innerHTML = 'error';
}

I have also code displaying progress bar for sites with more entertaining abilities.

柒七 2024-07-29 18:44:45

我完全同意,不幸的是没有跨浏览器的标准 API。 然而,即使存在,服务器也需要参与。 你不能相信你发送了多少数据,HTTP 需要确认。 因此,您需要服务器告诉您是否收到数据报#526。 如果有一个 API,这可能是一个很好的猜测,但实际上并不是同一件事:S

检查 www.faqs.org/rfcs/rfc1867.html

如果你可以 ping 服务器,你可以模拟它(没有信心)。

I completely agree, unfortunately there's no standard api for that across browsers. However even if that existed, the server would need to participate. You cannot trust how much data you have sent, HTTP needs an acknowledge. Therefore you would need the server telling you if datagram #526 is received. If there were an API it could be a good guess, but not really the same thing :S

Check www.faqs.org/rfcs/rfc1867.html

If you can ping the server, you could simulate it (with no confidence).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文