开发者

Download PDF file in Flex with HTTPService

Hi

Someone know how to download a pdf file with HTTPService?

I do that

private function downloadPDF():void
{
    var httpService:HTTPService = new HTTPService();
    httpService.url = "http://coenraets.org/flexandroid开发者_开发百科90/FlexAndroid90Minutes.pdf";
    httpService.addEventListener(ResultEvent.RESULT, result);
    httpService.addEventListener(FaultEvent.FAULT, fault);
    httpService.send();
}

private function fault(event:FaultEvent):void
{
    trace("Error");
    trace("\t> Code : "+event.fault.faultCode);
    trace("\t> Description : "+event.fault.faultString);
    trace("\t> Detail : "+event.fault.faultDetail);
}

private function result(event:ResultEvent):void
{
    var pdfFile:File = new File(File.applicationStorageDirectory.nativePath + File.separator + "myPDF.pdf");
    var fileStream:FileStream = new FileStream();
    fileStream.open(pdfFile, FileMode.WRITE);
    fileStream.writeUTFBytes(event.result as String);
    fileStream.close();
}

But I got an error

Error

Code : Client.CouldNotDecode

Description : Error #1090: XML parser failure: element is malformed.

Detail : null

With URLLoader it works fine

private function initPDF():void
{
    loader = new downloadPDF();
    loader.dataFormat = URLLoaderDataFormat.BINARY;
    var request:URLRequest = new URLRequest("http://coenraets.org/flexandroid90/FlexAndroid90Minutes.pdf");
    configureListeners(loader);
    try {
        loader.load(request);
    } catch (error:Error) {
        trace("Unable to load requested URL.");
    }
}

private function configureListeners(dispatcher:EventDispatcher):void {
    dispatcher.addEventListener(Event.COMPLETE, completeHandler);
    dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
}

private function completeHandler(event:Event):void {
    var pdfFile:File = new File(File.applicationStorageDirectory.nativePath + File.separator + "myPDF.pdf");
    var fileStream:FileStream = new FileStream();
    fileStream.open(pdfFile, FileMode.WRITE);
    fileStream.writeBytes(loader.data);
    fileStream.close();
}

private function ioErrorHandler(event:IOErrorEvent):void {
    trace("ioErrorHandler: " + event);
}


Sure you can.

You just need to add
httpService.resultFormat="text"

This will just give you the string response without parsing it as an xml. Though not recommended because HTTPService is meant for WebServices, it CAN be done


The thing is you can't download something that isn't text or XML (so, it's text) using HTTPLoader.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜