使用 HTTPService 在 Flex 中下载 PDF 文件
你好
有人知道如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然可以。
您只需要添加
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
问题是您无法使用 HTTPLoader 下载非文本或 XML 的内容(因此,它是文本)。
The thing is you can't download something that isn't text or XML (so, it's text) using HTTPLoader.