AS3開始,已經有了ByteArray這樣的類別
基本上只要知道編碼, 就可以使用flash來產生任何的檔案格式
zip應該是最常使用的
以前的下載檔案, 一次都只能一個
現在可以透過 aszip 類別, 將ByteArray編碼成zip格式
之後就可以丟給伺服器或使用FileReference的save,將檔案存出來。
http://code.google.com/p/aszip/

基本上只要知道編碼, 就可以使用flash來產生任何的檔案格式
zip應該是最常使用的
以前的下載檔案, 一次都只能一個
現在可以透過 aszip 類別, 將ByteArray編碼成zip格式
之後就可以丟給伺服器或使用FileReference的save,將檔案存出來。
http://code.google.com/p/aszip/

/**
* @author milkmidi
* @see http://milkmidi.blogspot.com
* @version 1.0.0
* @date created 2010/02/20/
*
* aszip
* http://code.google.com/p/aszip/
*/
package {
import com.bit101.components.InputText;
import com.bit101.components.PushButton;
import com.adobe.images.JPGEncoder;
import flash.display.*;
import flash.events.MouseEvent;
import flash.net.FileReference;
import flash.utils.ByteArray;
import org.aszip.zip.ASZip;
import org.aszip.compression.CompressionMethod;
import org.aszip.encoding.PNGEnc;
import org.aszip.saving.Method;
import org.aszip.saving.Download;
public class ASZipDemo extends Sprite {
[Embed(source = 'demo300x300.jpg')]
public static const Texture:Class;
private var _txtByte :ByteArray = new ByteArray();
private var myZip :ASZip;
private var input_txt :InputText;
public function ASZipDemo() {
myZip = new ASZip(CompressionMethod.GZIP);
new PushButton(this, 0, 0, "Generate", _saveZipHandler);
input_txt = new InputText(this, 0, 20, "A comment...");
input_txt.width = 400;
}
private function _saveZipHandler(e:MouseEvent):void {
var _bmp:BitmapData = Bitmap( new Texture()).bitmapData;
//var _imgBytes:ByteArray = PNGEnc.encode(_bmp);
var _imgBytes:ByteArray = new JPGEncoder(80).encode(_bmp);
//加入一段在壓 zip 檔時的注解說明文字。
myZip.addComment(input_txt.text);
//建立一個資料夾
myZip.addDirectory("pics/funk");
//建立一個名為text的資料夾
myZip.addDirectory("text");
//將 JPGEncoder 或 PNGEncoder 編碼過的 ByteArray 加入
myZip.addFile(_imgBytes, "pics/funk/milkmidi.jpg");
//加入文字至 ByteArray
_txtByte.writeUTFBytes("奶綠茶大家說 Hello !!");
//將文字的ByteArray 寫入到指定的txt檔裡。
myZip.addFile(_txtByte, "text/milkmidi.txt");
//將 zip 傳給伺服器
//myZip.saveZIP( Method.REMOTE , "createZip.ashx", Download.INLINE);
//使用 FileReference 直接將 ByteArray 存成實體的 zip 檔。
var _zipByte:ByteArray = myZip.saveZIP ( Method.LOCAL );
new FileReference().save(_zipByte, "aszip.zip");
}
}
}
.net端接ByteArray方法<%@ WebHandler Language="C#" Class="createZip" %>
using System;
using System.Web;
using System.IO;
public class createZip : IHttpHandler {
public void ProcessRequest (HttpContext context) {
Stream _stream = context.Request.InputStream;
string _fileName = context.Request["name"];
if (_stream.Length<=0) {
context.Response.End();
}
context.Response.ContentType = "application/zip";
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(_fileName));
byte[] _buffer = new byte[_stream.Length];
for (int i = 0; i < _stream.Length; i++) {
_buffer[i] = (byte)_stream.ReadByte();
}
_stream.Close();
context.Response.BinaryWrite(_buffer);
context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}
}
留言