4 Replies Latest reply on Jan 21, 2010 8:33 AM by JoelHH

    Casting File into FileReference

    JoelHH Level 1

      I'm wondering if it is possible to cast a File into a FileReference.  I would think since File is just an extension of FileReference that the extended information could be stripped and turned into a FileReference.  I want to do this in AIR so that I can pass the FileReference to my web based Flex application in order to upload a file.

       

      Right now this is what I am trying to do:

       

      var fileReference:FileReference = FileReference(file);

       

      The cast happens and then I pass it to my Flex web based app.  When I pass the new FileReference via a local connection I get this error:

       

      "Type Coercion failed: cannot convert Object@132df6c9 to flash.net.FileReference."

       

      Any ideas?  Let me know if you have any questions or need more information.  Thanks.

      fileReference:FileReference = FileReference(file);

        • 1. Re: Casting File into FileReference
          Gregory Lafrance Level 6

          By the time you get the "file" it is probably been cast to a generic Object, so try this:

           

          var fileReference:FileReference = FileReference(File(file));

           

          If this post answers your question or helps, please mark it as such.


          Greg Lafrance - Flex 2 and 3 ACE certified

          www.ChikaraDev.com

          Flex / AIR Development, Training, and Support Services

          1 person found this helpful
          • 2. Re: Casting File into FileReference
            JoelHH Level 1

            I tested this out and it is still giving the same error, that the coercion failed.

             

            The casting is occuring on the AIR side since there is no File class available in flex without AIR.  It seems like the cast occurs, but when I try to pass it to the web based application using a LocalConnection it is still being seen as File instead of a FileReference.  The full error that I receive is: "Error #2044: Unhandled AsyncErrorEvent:. text=Error #2095: flash.net.LocalConnection was unable to invoke callback uploadAttachments. error=TypeError: Error #1034: Type Coercion failed: cannot convert Object@13404cb9 to flash.net.FileReference." (the numbers & letters after the @ changes every execution).

             

            I can send it across the LocalConnection as an Object, but when I try to convert the Object into a FileReference on the web side I receive the same error.

             

            Any thoughts or suggestions?

            • 3. Re: Casting File into FileReference
              Gregory Lafrance Level 6

              In that case you might need to just create a new FileReference object in your AIR app, setting its properties as appropriate from the File object properties, then send the FileReference object to Flex.

               

              If this post answers your question or helps, please mark it as such.


              Greg Lafrance - Flex 2 and 3 ACE certified

              www.ChikaraDev.com

              Flex / AIR Development, Training, and Support Services

              1 person found this helpful
              • 4. Re: Casting File into FileReference
                JoelHH Level 1

                Unfortunately I don't believe I can set the properties from a File into a FileReference.  FileReference is a highly secured class, the only way to construct a new FileReference is by going through a browse and creating it from there.  This is why I was using the File class in the first place, to bypass the browse and then make it into a FileReference.

                 

                The issue seems to be with the LocalConnection class.  The cast in AIR is working fine, but when it is being passed across, it is being treated as an Object.  I guess non-basic types get treated as instances of Objects and cannot be explicitly cast.

                 

                Do you know if it's possible to create a FileReference from a File without casting?