使用 JavaScript 和 Google Gears 处理文件上传,有更好的解决方案吗?
所以 - 我已经使用这种文件上传方法一段时间了,但 Google Gears 对实现 HTML5 规范的较新浏览器的支持似乎很差。我听说过一些频道中出现过“已弃用”一词,因此我正在寻找替代品可以完成以下任务,并支持新的浏览器。我总是可以退回到齿轮/标准文件 POST,但以下项目使我的过程变得更加简单:
- 用户必须能够在对话框中选择多个文件进行上传。
- 我必须能够接收文件传输的状态更新。 (进度条)
- 我希望能够使用
PUT
请求而不是POST
- 我希望能够使用 JavaScript 轻松地将这些事件附加到现有 HTML 元素。 IE 文件选择应在
- 我希望能够使用 JavaScript 轻松控制响应/请求参数。
我不确定新的 HTML5 浏览器是否支持 gears 使用的桌面/请求对象,或者是否有一个 Flash 上传器具有我在 google 搜索中缺少的这些功能。
使用 gears 上传代码的示例:
// select some files:
var desktop = google.gears.factory.create('beta.desktop');
desktop.openFiles(selectFilesCallback);
function selectFilesCallback(files) {
$.each(files,function(k,file) {
// this code actually goes through a queue, and creates some status bars
// but it is unimportant to show here...
sendFile(file);
});
}
function sendFile(file) {
google.gears.factory.create('beta.httprequest');
request.open('PUT', upl.url);
request.setRequestHeader('filename', file.name);
request.upload.onprogress = function(e) {
// gives me % status updates... allows e.loaded/e.total
};
request.onreadystatechange = function() {
if (request.readyState == 4) {
// completed the upload!
}
};
request.send(file.blob);
return request;
}
编辑:显然 flash 无法使用 PUT 请求,因此我将其更改为“喜欢”而不是“必须”。
So - I've been using this method of file uploading for a bit, but it seems that Google Gears has poor support for the newer browsers that implement the HTML5 specs. I've heard the word deprecated floating around a few channels, so I'm looking for a replacement that can accomplish the following tasks, and support the new browsers. I can always fall back to gears / standard file POST's but these following items make my process much simpler:
- Users MUST to be able to select multiple files for uploading in the dialog.
- I MUST be able to receive status updates on the transmission of a file. (progress bars)
- I would like to be able to use
PUT
requests instead ofPOST
- I would like to be able to easily attach these events to existing HTML elements using JavaScript. I.E. the File Selection should be triggered on a
<button>
click. - I would like to be able to control response/request parameters easily using JavaScript.
I'm not sure if the new HTML5 browsers have support for the desktop/request objects gears uses, or if there is a flash uploader that has these features that I am missing in my google searches.
An example of uploading code using gears:
// select some files:
var desktop = google.gears.factory.create('beta.desktop');
desktop.openFiles(selectFilesCallback);
function selectFilesCallback(files) {
$.each(files,function(k,file) {
// this code actually goes through a queue, and creates some status bars
// but it is unimportant to show here...
sendFile(file);
});
}
function sendFile(file) {
google.gears.factory.create('beta.httprequest');
request.open('PUT', upl.url);
request.setRequestHeader('filename', file.name);
request.upload.onprogress = function(e) {
// gives me % status updates... allows e.loaded/e.total
};
request.onreadystatechange = function() {
if (request.readyState == 4) {
// completed the upload!
}
};
request.send(file.blob);
return request;
}
Edit: apparently flash isn't capable of using PUT requests, so I have changed it to a "like" instead of a "must".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(根据提问者的建议,从对原始问题的评论中衍生出来。)
Plupload 是最新的产品之一,它使用可用的最佳方法(HTML 5、Flash、Gears、Silverlight): http://plupload.com/
(Spawned from a comment on the original question, per askers's suggestion.)
Plupload is one of the newest kids on the block and it uses the best method available (HTML 5, Flash, Gears, Silverlight): http://plupload.com/
我以前使用过基于 Flash 的,例如 http:// www.webresourcesdepot.com/open-source-flash-uploader-multi-bit-shift/。它们工作正常,并且仅依赖于闪存应该可以在大多数计算机上工作。我还没有找到一种可靠的方法来单独使用 js 来实现这一点,但是 flash 可以用 js 来控制 - 特别是如果你自己编写的话。
希望这有帮助。
I have previously used flash based ones such as http://www.webresourcesdepot.com/open-source-flash-uploader-multi-bit-shift/. They work ok and being only dependent on flash should work on most computers. I haven't found a reliable way of doing it with js alone but flash could be controlled with js - especially if you write your own.
hope this helps.