6 Replies Latest reply on Jan 10, 2008 4:09 AM by deepres

    File Upload and HTTP service

    deepres
      Hello all,

      I have a problem while communicating back values after file upload. I have my flex application with HTTP Service registered which is my servlet on the server side. When I'm processing normal event, everything is fine. In my mxml file I have:

      <mx:HTTPService id="textRequest"
      method="post"
      url=" http://localhost:8080/myservice"
      useProxy="false"
      resultFormat="e4x"
      result="onResult(event);"
      fault="onFault(event);" invoke="onInvoke(event)">
      </mx:HTTPService>

      Result method is registered and it's processing all of my server data. But this method is not being invoked when I'm doing file upload, any clues why ?? After file upload I need to communicate back the content of uploaded file, and I just wanted to put that into the server's answer. Just for the record, beside that file upload is working fine.

      Thanks for help,
      Cheers
        • 1. Re: File Upload and HTTP service
          atta707 Level 2
          so how are your writing the contents of your file back once it has been successfully received by the servlet? Are you writing all the bytes back to response? it that's the case, you result event you fire normally and receive the data sent from the server.

          ATTA
          • 2. Re: File Upload and HTTP service
            deepres Level 1
            On the server side, after getting the file, I'm creating xml in a form:

            <message>
            <event name="upload"/>
            <data text="theConentOfFile"/>
            </message>

            Then, I'm sending that back in doPost method:

            httpServletResponse.setContentType("text/xml");
            PrintWriter out = httpServletResponse.getWriter();

            Document doc = new Document(myXMLelement);
            XMLOutputter serializer = new XMLOutputter();
            serializer.output(doc, out);
            out.close();

            So, I guess that this content is sent back to the client, but I don't know how to access this - normally, I'm doing that in onResult(ev:ResultEvent) function, but in this case this one is not invoked.
            • 3. Re: File Upload and HTTP service
              m_hartnett Level 3
              I am assuming that your file upload is using a fileReference object to do the operation.

              Add the Complete handler to the fileReference object doing the upload.
              fileRef.addEventListener(Event.COMPLETE, this.uploadCompleteHandler);
              • 4. Re: File Upload and HTTP service
                deepres Level 1
                I tried to use this solution too. Correct me if I'm wrong but complete handler takes different event type as an argument, so I can't access desired values like I'm doing it now in my onResult method:

                event.result.message

                Is there a workaround for that ?
                • 5. File Upload and HTTP service
                  m_hartnett Level 3
                  You can also use a listener for the HTTPStatusEvent. It is dispatched when an upload fails because of an HTTP error.

                  This is how all of our uploads work. The only difference is that we are using a URLRequest. However, that should not be an issue because Flex turns the call into an HTTP service call once it is made. Hope this helps

                  if(locationForm.uploadRequired) {
                  locationForm.fileRef.addEventListener(Event.COMPLETE, this.uploadCompleteHandler);
                  var request:URLRequest = new URLRequest(" http://localhost:8090/flexmed/locationUpload.flex")
                  locationForm.fileRef.upload(request,"FileData");

                  public function uploadCompleteHandler(e : Event ) : void {

                  try {
                  saveFormData(e);
                  }
                  catch (error:Error) {
                  trace("Unable to upload file.");
                  }

                  }
                  • 6. Re: File Upload and HTTP service
                    deepres Level 1
                    Hi,

                    I think, I found the optmial solution for me. In my case I didn't want to double the number of communicates, so here it is, the upload data complete event and an example code for that:

                    dispatcher.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadCompleteDataHandler);

                    private function uploadCompleteDataHandler(event:DataEvent) :void {

                    var result:XML = new XML(event.data);
                    dataString = result.data.@text;

                    }

                    event.data contains "raw server response" so you can fetch your data from there.

                    Thanks for your help :)

                    P.S. I found some info on that here:
                    http://www.dgrigg.com/post.cfm/08/02/2007/Flex-and-Flash-file-uploading-with-return-data