使用 HTTPService 在 Flex 中下载 PDF 文件

发布于 2024-12-03 18:38:48 字数 2189 浏览 1 评论 0原文

你好
有人知道如何使用 HTTPService 下载 pdf 文件吗?
我这样做

private function downloadPDF():void
{
    var httpService:HTTPService = new HTTPService();
    httpService.url = "http://coenraets.org/flexandroid90/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();
}

但是我得到了一个错误

错误
代码:Client.CouldNotDecode
描述:错误 #1090:XML 解析器失败:元素格式错误。
详细信息:空

使用 URLLoader 可以正常工作

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

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/flexandroid90/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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

后eg是否自 2024-12-10 18:38:48

当然可以。

您只需要添加
httpService.resultFormat="text"

这只会为您提供字符串响应,而不将其解析为 xml。虽然不推荐,因为 HTTPService 是用于 Web 服务的,但它可以完成

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

甜妞爱困 2024-12-10 18:38:48

问题是您无法使用 HTTPLoader 下载非文本或 XML 的内容(因此,它是文本)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文