protected function downloadImage():void
{
loadImage();
}
public function loadImage():void
{
var NowDate:Date=new Date();
var dateStr:String=String(NowDate.date)+'-'+String(NowDate.month)+'-'+
String(NowDate.fullYear)+' '+String(NowDate.hours)+'-'+String(NowDate.minutes)+'-'+String(NowDate.seconds);
var file:File= File.desktopDirectory.resolvePath('image_'+dateStr);;
file.addEventListener(Event.SELECT,onSelect);
file.browseForSave("Save image");
}
public var filePath:String='';
public var fileName:String='';
public function onSelect(event:Event):void
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadcompleteHandler);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioError);
loader.load(new URLRequest(img_path));
fileName=event.target.name;
filePath=event.target.url;
}
private var _bitmapData:BitmapData;
public function loadcompleteHandler(event:Event):void
{
var loaderInfo:LoaderInfo = event.target as LoaderInfo;
var bitmap:Bitmap = loaderInfo.content as Bitmap;
_bitmapData = bitmap.bitmapData;
var imgByteArray:ByteArray;
var jpegEncoder:JPEGEncoder = new JPEGEncoder(80);
imgByteArray = jpegEncoder.encode(_bitmapData);
var file:File = new File(filePath+'.jpg');
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(imgByteArray);
fileStream.close();
pop.status_text("Photo Downloaded Successfully",1);
}
謝謝您的回覆,Pirvulescu。我可以做的是在這裏添加一個支票,並警告用戶輸入帶擴展名的整個文件名。我正在尋找一種更加舒適的方式來自動添加文件擴展名。有人可以建議如何做到這一點? – xuchen 2010-07-14 06:02:35