File compression before upload on the client-side(在客户端上传之前的文件压缩)
问题描述
基本上我将处理大型 XML 文件(大约 20 - 50 MB).这些文件需要上传到服务器上.
Basically I'll be working with large XML files (approx. 20 - 50 MB). These files needs to be uploaded on a server.
我知道不可能用 javascript 访问文件,也不能在客户端实现 HTTP 压缩.
I know it isn't possible to touch the files with javascript, nor to implement HTTP compression on the client-side.
我的问题是,是否存在压缩文件并具有 javascript API 的解决方案(闪存/动作脚本)?
My question is that if any solution exists (flash / action script) that compresses a file and has a javascript API?
场景是这样的:
- 尝试上传 50 MB XML 文件
- 在上传之前使用 Javascript 抓取它并将其发送到压缩器.
- 上传压缩文件而不是原始文件.
推荐答案
Flash 内置的 ByteArray 实现有一个方法(ByteArray::deflate
对(字节数组的)内容进行放气)放气算法是DEFLATE 压缩数据格式规范 1.3 版.
Flash's inbuilt implementation of ByteArray has a method (ByteArray::deflate
to deflate the contents (of the bytearray) The deflate algorithm is the DEFLATE Compressed Data Format Specification version 1.3.
还有一个 ByteArray::compress
方法,它使用 zlib 算法
There;s also a ByteArray::compress
method which compresses using the zlib algorithm
等一下,我会给你写一些示例代码来使用这个类并将它暴露给 JavaScript.
Hold on a bit, I'll write you some sample code to use this class and expose it to JavaScript.
编辑
我已将文件上传到 http://www.filefactory.com/file/cf8a39c/n/demo5.zip
EDIT 2对于那些无法下载文件的人:
EDIT 2 For those who couldn't download the files:
我在 demo5.fla 中的 ActionScript 代码(编译为 demo5.swf)
My ActionScript code in demo5.fla (compiled to demo5.swf)
import flash.external.ExternalInterface;
import flash.net.FileReference;
import flash.events.Event;
import flash.utils.ByteArray;
if(ExternalInterface.available) {
//flash.system.Security.allowDomain("localhost");
ExternalInterface.addCallback("deflate", doDeflate);
ExternalInterface.addCallback("compress", doCompress);
}
var method:String="deflate";
var b:ByteArray;
function doCompress(_data:String):void {
method="compress";
exec(_data);
}
function doDeflate(_data:String):void {
method="deflate";
exec(_data);
}
function exec(_data:String):void {
b=new ByteArray();
b.writeUTFBytes(_data);
b.position=0;
if(method=="compress") {
b.compress();
} else if(method=="deflate") {
b.deflate();
}
executed();
}
function executed():void {
if(ExternalInterface.available) {
b.position=0;
var str:String=b.readUTFBytes(b.bytesAvailable);
ExternalInterface.call("onExec", str);
}
}
我嵌入 swf 的 HTML 代码:
My HTML code to embed the swf:
<button onclick="doDeflate()">Deflate</button>
<button onclick="doCompress()">Compress</button>
<div id="flashContent">
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="1" height="1" id="demo5" align="middle">
<param name="movie" value="demo5.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<param name="play" value="true" />
<param name="loop" value="true" />
<param name="wmode" value="window" />
<param name="scale" value="showall" />
<param name="menu" value="true" />
<param name="devicefont" value="false" />
<param name="salign" value="" />
<param name="allowScriptAccess" value="always" />
<embed src="demo5.swf" quality="high" bgcolor="#869ca7"
width="1" height="1" name="demo5" align="middle"
play="true" loop="false" quality="high" allowScriptAccess="always"
type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer">
</embed>
</object>
</div>
最后是javascript代码:
and finally the javascript code:
function doDeflate() {
var data="fdg fhnkl,hgltrebdkjlgyu ia43uwriu67ri8m nirugklhvjsd fgvu";
//DATA CONTAINS DATA TO BE DEFLATED
thisMovie("demo5").deflate(data);
}
function doCompress() {
var data="fdg fhnkl,hgltrebdkjlgyu ia43uwriu67ri8m nirugklhvjsd fgvu";
//DATA CONTAINS DATA TO BE DEFLATED
thisMovie("demo5").compress(data);
}
function onExec(data) {
//DATA CONTAINS THE DEFLATED DATA
alert(data);
}
function thisMovie(movieName) {
if (navigator.appName.indexOf("Microsoft") != -1) {
return window[movieName];
} else {
return document[movieName];
}
}
这篇关于在客户端上传之前的文件压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!