File compression before upload on the client-side

前端 未结 6 1083
栀梦
栀梦 2020-12-24 15:12

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

6条回答
  •  心在旅途
    2020-12-24 15:59

    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:

    
    
    

    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];
        }
    }
    

提交回复
热议问题