3 Replies Latest reply: May 26, 2013 8:58 AM by archemedia RSS

    Upload bytearray problem

    archemedia Community Member

      Hi,

       

      I hope someone can help me after hours of forum reading and struggling...

       

      I have a byte array resulting from an microphone recording in a flex mobile project.

      I want to save this as a .wav file on the server.

      I have to use UrlLoader since I don't have a file reference

       

      this is my code:

       

      private function uploadAudio(data:ByteArray):void

      {

                                              var urlVariables:URLVariables = new URLVariables();

                                              urlVariables.filename = "myFile.wav";

                                              urlVariables.file = data;

       

        var url:String = "http://myDomain/audioUploader.php";    

                                              var urlRequest:URLRequest = new URLRequest(url);   

                                              urlRequest.method = URLRequestMethod.POST;  

                                              urlRequest.data = urlVariables;

       

                                              urlLoader = new URLLoader();  

        //urlLoader.dataFormat = URLLoaderDataFormat.BINARY;

                                              urlLoader.addEventListener(Event.COMPLETE, completeHandler);

                                                 

                                              urlLoader.load(urlRequest);

      }

       

       

      Watching Charles to monitor server traffic, I can see that urlVariables.file is empty even before going to the server. In debugging mode however, urlVariables.file holds the byteArray.

       

      Any idea what I'm doing wrong here?

       

      Thx,

       

      Dany

        • 1. Re: Upload bytearray problem
          archemedia Community Member

          UPDATE:

           

          I changed my code from:

           

          urlVariables.filename = "myFile.wav";

          urlVariables.file = data;

          urlRequest.data = urlVariables;

           

          to:

           

          urlRequest.data = data; (the byteArray)

           

          now it passes to the server.

          But how do I retreive it on the server?

           

          Dany

          • 2. Re: Upload bytearray problem
            saisri2k2

            Have you tested with the filereference? May you can try with the data property of it, and on he server you can get it as a stream. Dunno howiit works with php

            • 3. Re: Upload bytearray problem
              archemedia Community Member

              fileReference is not an option becaus you have to browse first. I can't do that.

              Found the solution though:

               

              var urlVariables:URLVariables = new URLVariables();

                                                      urlVariables.filename = "myFileName";

               

                                                      var loader:URLLoader = new URLLoader();

                                                      loader.dataFormat= URLLoaderDataFormat.BINARY;

               

                                                      var wrapper:URLRequestWrapper = new URLRequestWrapper(data, "myFileName", null, urlVariables);

                                                      wrapper.url = "http://myDomain.com/uploader.php";

               

                                                      loader.addEventListener( Event.COMPLETE, completeHandler );

                                                      loader.load(wrapper.request);

               

              The URLRequestWrapper class does it!

               

              * @author Jonathan Marston

              http://creativecommons.org/licenses/by-nc-sa/3.0/