在客户端上载前进行文件压缩 [英] File compression before upload on the client-side
问题描述
基本上,我将使用大型XML文件(大约20 - 50 MB)。这些文件需要上传到服务器上。
我知道使用javascript无法触摸文件,也不能在客户端实现HTTP压缩。
我的问题是,如果存在压缩文件并有JavaScript API的任何解决方案(flash / action script)?
这种情况:
- 尝试上传50 MB XML文件
- 上传压缩文件,而不是原始文件。
Flash内置的ByteArray实现有一个方法( ByteArray :: deflate
bytearray)缩尺算法是 DEFLATE压缩数据格式规范版本1.3 。
还有一个 ByteArray :: compress
方法,使用 zlib 算法
稍等一下,我会给你写一些示例代码使用此类并将其公开到JavaScript。
编辑
请在 http://www.filefactory.com/file/cf8a39c/n上上传该文件/demo5.zip
编辑2 对于无法下载文件的用户:
我在demo5.fla中的ActionScript代码(编译为demo5.swf)
import flash.external。外部接口;
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);
}
函数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);
}
}
我的HTML代码嵌入swf:
< button onclick =doDeflate()> Deflate< / button>
< button onclick =doCompress()> Compress< / button>
< div id =flashContent>
< object classid =clsid:d27cdb6e-ae6d-11cf-96b8-444553540000width =1height =1id =demo5align =middle>
< param name =movievalue =demo5.swf/>
< param name =qualityvalue =high/>
< param name =bgcolorvalue =#ffffff/>
< param name =playvalue =true/>
< param name =loopvalue =true/>
< param name =wmodevalue =window/>
< param name =scalevalue =showall/>
< param name =menuvalue =true/>
< param name =devicefontvalue =false/>
< param name =salignvalue =/>
< param name =allowScriptAccessvalue =always/>
< embed src =demo5.swfquality =highbgcolor =#869ca7
width =1height =1name =demo5align = middle
play =trueloop =falsequality =highallowScriptAccess =always
type =application / x-shockwave-flash
pluginspage =http ://www.macromedia.com/go/getflashplayer>
< / embed>
< / object>
< / div>
,最后是javascript代码:
function doDeflate(){
var data =fdg fhnkl,hgltrebdkjlgyu ia43uwriu67ri8m nirugklhvjsd fgvu;
//数据包含要定义的数据
thisMovie(demo5)。deflate(data);
}
function doCompress(){
var data =fdg fhnkl,hgltrebdkjlgyu ia43uwriu67ri8m nirugklhvjsd fgvu;
//数据包含要定义的数据
thisMovie(demo5)。
}
函数onExec(data){
//数据包含定义数据
alert(data);
}
function thisMovie(movieName){
if(navigator.appName.indexOf(Microsoft)!= -1){
return window [movieName] ;
} else {
return document [movieName];
}
}
Basically I'll be working with large XML files (approx. 20 - 50 MB). These files needs to be uploaded on a server.
I know it isn't possible to touch the files with javascript, nor to implement HTTP compression on the client-side.
My question is that if any solution exists (flash / action script) that compresses a file and has a javascript API?
The scenario is this:
- Trying to upload 50 MB XML file
- Before upload a grab it with Javascript and send it to the compressor.
- Upload the compressed file instead of the original one.
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.
There;s also a ByteArray::compress
method which compresses using the zlib algorithm
Hold on a bit, I'll write you some sample code to use this class and expose it to JavaScript.
EDIT
I've uploaded the file at http://www.filefactory.com/file/cf8a39c/n/demo5.zip
EDIT 2 For those who couldn't download the files:
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);
}
}
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>
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];
}
}
这篇关于在客户端上载前进行文件压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!