尚未发现Flex中有可以直接渲染HTML代码的组件, 但可通过服务器端配合实现预览.
原理: Flex(尤指AIR环境), 使用URLLoader发送POST请求, 将要预览的HTML代码发送到服务器端Servlet, 服务器端收到请求后创建临时文件, 将HTML存到临时文件中. Flex端收到URLLoader Complete事件后, 使用navigateToURL navigate到Servlet上(使用GET), Servelet 返回文件内容 —- 在上述过程中, Flex端生成UUID作为代码读写的key.
权限验证:
var fileRef:FileReference = new FileReference(); var request:URLRequest = new URLRequest(serviceUrl); request.method = URLRequestMethod.POST; var encoder : Base64Encoder = new Base64Encoder(); encoder.encode(userName + ":" + password); request.requestHeaders.push(new URLRequestHeader("Authorization", "Basic " + encoder.toString())); // ** 增加认证信息 fileRef.addEventListener(Event.OPEN, onFileDownloadBegin); fileRef.addEventListener(Event.COMPLETE, onFileDownloadComplete); fileRef.addEventListener(IOErrorEvent.IO_ERROR, onFileDownloadError); fileRef.addEventListener(Event.CANCEL, onFileDownLoadCancel); fileRef.download(request, getExportFileName(serviceUrl));
使用URLRequest下载文件. – 无权限认证
// 下载文件 protected function downloadTemplateFile():void { var fileRef:FileReference = new FileReference(); var urlReq:URLRequest = new URLRequest(_pathTemplateFile); fileRef.addEventListener(Event.OPEN,onDownloadBegin); fileRef.addEventListener(Event.COMPLETE, onDownloadComplete); fileRef.addEventListener(Event.CANCEL, onDownloadCancel); fileRef.addEventListener(IOErrorEvent.IO_ERROR, onDownloadError); fileRef.download(urlReq); } // 模板文件下载开始时响应. protected function onDownloadBegin(e:Event):void { appendTextToOutPut(RM.getString(BUNDLE_ASMT_MGT, "template.download.begin")); } // 当下载结束后响应. */ protected function onDownloadComplete(e:Event):void { var file:FileReference = e.target as FileReference; appendTextToOutPut(RM.getString(BUNDLE_ASMT_MGT, "template.download.success", [file.name, FileUtils.formatFileSize(file.size)])); log.info("模板文件下载结束: " + file.name + "-" + file.size); } // 当下载取消时响应. */ protected function onDownloadCancel(e:Event):void { appendTextToOutPut(RM.getString(BUNDLE_ASMT_MGT, "controller.download.canceled")); } // 当下载出现错误时响应. */ protected function onDownloadError(e:IOError):void { appendTextToOutPut(RM.getString(BUNDLE_ASMT_MGT, "template.download.error", [e.message])); log.error("下载出错" + e.message); }
// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.