Quantcast
Viewing latest article 24
Browse Latest Browse All 21156

How to open a PDF File from a ByteArray

Hi, Fellows,
 
I'm having the following problem:
 
I have put a PDF file inside a BLOB field in a table in my SQLite database.
 
And I can retrieve it from this database without any problem, receiving a ByteArray data type as result.
 
What I want to know is what I should do to render this file in my HTML flex object without saving it to a file…
 
I used the mx.controls.HTML.data property, but what I see is the TEXT of my PDF file. The mx.controls.HTML.location needs a STRING with the link to a URL site or file.
 
Thanks in advance for your help.
 
Here, my code:
 
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" width="810" height="620"
      creationComplete="creationCompleteHandler();">
      <mx:Script>
            <![CDATA[
                  import flash.data.*;
                  import flash.filesystem.*;
 
                   private var _connection:SQLConnection;
                  private var _statement:SQLStatement; 
                   
             private function creationCompleteHandler():void {
                var file:File = File.applicationStorageDirectory.resolvePath("arq.db");
                _connection = new SQLConnection();
                _connection.addEventListener(SQLEvent.OPEN, openHandler);
                _connection.openAsync(file, SQLMode.CREATE);
            }
            
             private function openHandler(event:SQLEvent):void {
                var sql:SQLStatement = new SQLStatement();
                sql.sqlConnection = _connection;
                sql.text = "CREATE TABLE IF NOT EXISTS arq(" +
                           "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                           "nome TEXT, " + 
                            "arquivo BLOB)";
                sql.execute();        
             } 
             
             private function insertFile(identificacao:String, caminho:String):void {
                        var sql:SQLStatement = new SQLStatement();
                        sql.sqlConnection = _connection;
                        var arquivo:File = new File(caminho);
                        var arqStream:FileStream = new FileStream();
                        arqStream.open(arquivo,SQLMode.READ);
                        var arqBlob:ByteArray = new ByteArray();
                        arqStream.readBytes(arqBlob);
                        arqStream.close();
                sql.text = "INSERT INTO arq(nome, arquivo)" + 
                            "VALUES(@nome, @arquivo)";
                sql.parameters["@nome"] = identificacao;
                sql.parameters["@arquivo"] = arqBlob;
                sql.execute();
                
                 trace("Arquivo inserido com Sucesso!!!");
                
                 lb1.text = "Arquivo inserido com Sucesso!!!";
                  
             }   
             
             private function selectFile(identificacao:String):void {
                  
                         var sql:SQLStatement = new SQLStatement();
                        sql.sqlConnection = _connection;
                sql.text = "SELECT id, arquivo FROM arq WHERE nome=@nome";
                sql.parameters["@nome"] = identificacao;
                trace(sql.text);
                sql.addEventListener(SQLEvent.RESULT, apresentarDados);
                sql.execute();
 
             }
            
             private function apresentarDados(event:SQLEvent):void {
 
                         var statement:SQLStatement = event.target as SQLStatement;
                        var result:SQLResult = statement.getResult();
                        if(result != null && result.data != null) {
                              var dataset:Array = result.data;
                              trace(dataset.length);
                        var arqBlob:ByteArray = dataset[0].arquivo;
                
                   var xx:HTMLLoader = new  HTMLLoader();
                  var ur:URLRequest = new URLRequest();
                  var ul:URLLoader = new URLLoader();
                  
                   //Right now, it's doing nothing
ur.contentType = "application/pdf";
                  
                   ul.dataFormat = URLLoaderDataFormat.BINARY;
                  ul.data = arqBlob;
                                    
                   //Here is my problem - WHAT SHOULD I DO?
pdfFile.data = arqBlob;
            
                       trace("Cheguei!!!");
                      lb1.text = "Cheguei!!!";
                              
                         } else {
                              trace("Não funcionou!!!")
                        }
                  
             }
            
             private function sair():void {
                  this.exit();
            }
                  
             ]]>
      </mx:Script>
      <mx:TextInput x="99" y="10" id="arq"/>
      <mx:Label x="10" y="12" text="Identificação:" width="81"/>
      <mx:Button x="168" y="40" label="Apresentar" click="selectFile(arq.text)"/>
      <mx:Button x="728" y="10" label="Sair" width="60" click="sair()"/>
      <mx:TextInput x="417" y="10" id="id1"/>
      <mx:TextInput x="417" y="40" id="cm1"/>
      <mx:Button x="585" y="12" label="Gravar" click="insertFile(id1.text, cm1.text)"/>
      <mx:Label x="291" y="12" text="Identificação:" width="105"/>
      <mx:Label x="291" y="42" text="Caminho Completo:" width="118"/>
      <mx:Label x="615" y="42" width="173" id="lb1"/>
      <mx:HTML id="pdfFile" width="800" height="520" y="79"/>
 
</mx:WindowedApplication>


Viewing latest article 24
Browse Latest Browse All 21156

Trending Articles