使用Javascript:跨浏览器的无服务器文件上传和下载 [英] Javascript: cross-browser serverless file upload and download

查看:182
本文介绍了使用Javascript:跨浏览器的无服务器文件上传和下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我工作的一个Web应用程序,其中用户将需要:

So I'm working on a web app where a user will need to:


  • 提供全面的数据文件,以对工作

  • 其结果保存到一个文件

所有的操作是在JavaScript中完成的,所以我真的没有需要服务器端code,但(只是静态的主机),我喜欢这种方式。

All the manipulation is done in javascript, so I don't really have a need for server-side code yet (just static hosting), and I like it that way.

在Firefox中,我可以用自己的文件操作API 以允许用户上传文件直接进入客户端code(使用标准<输入类型=文件/> ),并创建一个对象URL出文件,这样用户就可以保存由客户端code创建的文件。

In Firefox, I can use their file manipulation api to allow a user to upload a file directly into the client-side code (using a standard <input type=file/>) and create an object URL out of a file so a user can save a file created by the client-side code.

<input type="file" id="input" onchange="handleFiles(this.files)">
<a download="doubled" id="ex">right-click and save as</a>
<script>
  function handleFiles(fileList){
    var builder = new MozBlobBuilder();
    var file = fileList[0];
    var text = file.getAsBinary();
    builder.append(text);
    builder.append(text);
    document.getElementById('ex').href = window.URL.createObjectURL( builder.getBlob() );
  }
</script>

因此​​,这是伟大的。现在我想这样做在其他浏览器 - 或者,至少,其他浏览器的现代版本。做类似的API存在让Chrome和IE?如果是这样,有没有人已经建成,我应该使用一个跨浏览器的包装?

So this is great. Now I want to do the same in other browsers - or, at least, modern versions of other browsers. Do similar APIs exist for Chrome and IE? If so, has anyone already built a cross-browser wrapper that I should be using?

推荐答案

这主要是可在Firefox 3.6+,Chrome浏览器10+,歌剧11.1+,并希望Safari浏览器6和IE 10。

It's mostly available on Firefox 3.6+, Chrome 10+, Opera 11.1+, and hopefully Safari 6 and IE 10.

请参阅: http://caniuse.com/#search=FileReader

这篇关于使用Javascript:跨浏览器的无服务器文件上传和下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆