0 Replies Latest reply on Dec 14, 2011 8:22 PM by ros_c

    Help on uploading and downloading of file to/from database.

    ros_c

      Hi everyone,

        I have problem with the uploading and downloading of files to/from database(store as BLOB) using Flex. I have coded the upload and download function my mxml file as shown below. Is the correct way of doing it ? Do i need to convert anything when i pass the file into my java file ? Cause when I try to download the file and save in my local and when I try to open the file , it actually contain the Content-Deposition n Content-Type in my data file. Any idea how to resolve it?

       

       

      Uploading

      <mx:HBox> 

         <mx:Button label="Upload" click="upload('bailBank');"/> 

        </mx:HBox>

       

      private function upload (bailBank:String):void { 

          if (file.size > 0) { 

           var request:URLRequest = new URLRequest("http://xxxx/UploadFileServlet?referenceNumber="+referenceNum+"&referenceNu="+bailBank+"&fi leName="+file.name);

           file.upload(request); 

          } 

         }

       

      java

                  ServletInputStream is = req.getInputStream();

                   byte[] bailkFile=InputStreamToByte(is);

                    entity.setBailBankstFile(Hibernate.createBlob(bailkFile));  -->calling my insert statement

       

      Downloading

      <mx:HBox> 

         <mx:Button label="View" click="download('bailBank');"/> 

        </mx:HBox>

       

      private function download (bailBank:String):void { 

           var servleturl="http://xxx/UploadFileServlet?referenceNumber="+referenceNum+"&referenceNu="+bailBank;

           var urlRequest:URLRequest = new URLRequest(servleturl); //servleturl是servlet访问地å&#157;€

           urlRequest.method=URLRequestMethod.GET;

           urlRequest.contentType="multipart/form-data";

           var fileRef:FileReference = new FileReference();

           fileRef.addEventListener(Event.COMPLETE, file_complete); 

           fileRef.addEventListener(ProgressEvent.PROGRESS, file_progress); 

           fileRef.download(urlRequest,"mail.txt");

         }

       

      java

      OutputStream output = resp.getOutputStream();

      Object object = entity.getBailBankstFile();

      byte[] attachmentFile = null;

         if (object != null && object instanceof byte[]) {

          attachmentFile = (byte[]) object;

         }

         attachmentFile=blobToBytes(famBehaDTO.getBailBankstFile());

         InputStream stream = null;