2008-08-04

Download a file from a server with AIR

Been working allot in AIR lately and I just got to the point of adding a feature where a user can download a file from the server. Initially I thought the following simple code would do it:

var req:URLRequest = new URLRequest( "http://localhost:8080/orion/pdfdown" );
req.method = URLRequestMethod.POST;
var f:File = new File();
f.download( req, "bla.pdf" );

However running the application with that causes the following error to be thrown: Error #2044: Unhandled IOErrorEvent:. text=Error #2038: File I/O Error. I really don't understand and after some searching I couldn't find the answer, so I decided to post on Flexcoders, and thanks to Ryan Gravener for posting the solution. In AIR you should use the URLStream class to download files from the server. Like so:

var req:URLRequest = new URLRequest(url);
stream = new URLStream();
stream.addEventListener(Event.COMPLETE, writeAirFile);
stream.load(req);

private function writeAirFile(evt:Event):void {
var data:ByteArray = new ByteArray();
stream.readBytes(fileData,0,stream.bytesAvailable);
var file:File = File.documentsDirectory.resolvePath("bla.pdf");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(fileData,0,fileData.length);
fileStream.close();
}

10 comments:

Darla said...

Good post.

Bryson said...

Thanks, this was quite useful for me.

Daniele said...

Thanks, good post, I was looking for the way to do it.
Just a little bug now that i test it, it should be:

var fileData:ByteArray = new ByteArray();

Anonymous said...

酒店打工

酒店兼職

台北酒店

打工兼差

酒店工作

酒店經紀

禮服店

酒店兼差

酒店上班

酒店PT

酒店

酒店喝酒

酒店消費

喝花酒

粉味

酒店上班請找艾葳 said...

艾葳酒店經紀公司提供專業的酒店經紀, 酒店上班小姐,八大行業,酒店兼職,傳播妹,或者想要打工兼差打工,兼差,八大行業,酒店兼職,想去酒店上班, 日式酒店,便服店,制服酒店,ktv酒店,禮服店,整天穿得水水漂漂的,還是想去制服店日領上班小姐,水水們如果想要擁有打工工作、晚上兼差工作兼差打工假日兼職兼職工作酒店兼差兼差打工兼差日領工作晚上兼差工作酒店工作酒店上班酒店打工兼職兼差兼差工作酒店上班等,想了解酒店相關工作特種行業內容,想兼職工作日領假日兼職兼差打工、或晚班兼職想擁有鋼琴酒吧又有保障的工作嗎???又可以現領請找專業又有保障的艾葳酒店經紀公司!

艾葳酒店經紀是合法的公司工作環境高雅時尚,無業績壓力,無脫秀無喝酒壓力,高層次會員制客源,工作輕鬆,可日領現領
一般的酒店經紀只會在水水們第一次上班和領薪水時出現而已,對水水們的上班安全一點保障都沒有!艾葳酒店經紀公司的水水們上班時全程媽咪作陪,不需擔心!只提供最優質的酒店上班,酒店上班,酒店打工環境、上班條件給水水們。心動嗎!? 趕快來填寫你的酒店上班履歷表

水水們妳有缺現領、想要兼職、有缺錢的煩腦嗎?想到日本留學缺錢嗎?妳是傳播妹??想要擁有高時薪又輕鬆的賺錢,酒店和,假日打工,假日兼職賺錢的機會嗎??想實現夢想卻又缺錢沒錢嗎!??
艾葳酒店台北酒店經紀招兵買馬!!徵專業的酒店打工,想要去酒店的水水,想要短期日領,酒店日領,禮服酒店,制服店,酒店經紀,ktv酒店,便服店,酒店工作,禮服店,酒店小姐,酒店經紀人,
等相關服務 幫您快速的實現您的夢想~!!

Anonymous said...

Can we detect the url of the clicked link?

I want to force download 1 file clicking on url.

Pls heml

Ari said...

great article! thanks!

Anonymous said...

酒店打工 酒店兼職
台北酒店 打工兼差 酒店工作 禮服酒店
酒店兼差 酒店上班 酒店應徵 酒店 酒店經紀

gaohui said...

huhu

Anonymous said...

To be a adroit benign being is to procure a amiable of openness to the world, an cleverness to group undeterminable things beyond your own control, that can take you to be shattered in unequivocally outermost circumstances on which you were not to blame. That says something exceedingly impressive relating to the prerequisite of the righteous autobiography: that it is based on a trust in the fitful and on a willingness to be exposed; it's based on being more like a plant than like a prize, something rather dainty, but whose mere precise beauty is inseparable from that fragility.