5 Replies Latest reply on Mar 15, 2012 6:39 AM by Maximist

    How to know the content type of the file Reference

    Maximist Level 1

      Hallo. I am using this code for uploading a file on server:

       

      protected function sfogliaImmagineHandler_moddat(event:MouseEvent):void

      {

           this.fotoStruttura = new FileReference();

           var filtroImmagini:FileFilter = new FileFilter("Immagini", "*.jpg");

           this.fotoStruttura.browse([filtroImmagini]);

           this.fotoStruttura.addEventListener(Event.SELECT, this.fileSelected);

      }

                                                    

      private function fileSelected(event:Event):void

      {

            this.fotoStruttura.load();

            this.fotoStruttura.addEventListener(Event.COMPLETE, this.fileLoaded);         

      }

                                                    

      private function fileLoaded(event:Event):void

      {

           .......

      }

       

      As you see i load the image and i filter jpg images and that's ok.. Anyway i would like to know what type of file the user is uploading cause i can rename my file.exe in file.jpg and nothing will be intercepted by my "jpg" filter.. How can i know that the file is an exe file and not a jpg file??.. Do i have a useful property or something similar??

       

      Thx in advance

       

      Max

        • 2. Re: How to know the content type of the file Reference
          Claudiu Ursica Level 4

          A possible solutions depends on what file types you want to allow to be uploaded.

           

          For simplicity I will assume that only jpegs.

           

          You will need to scan the loaded bytes and look for valid Jpeg headers. the bytes that are supposed to he in headers are not compliant to the jpeg specs reject the file.

           

           

          Just to sleep better at night, do the same server side if you need to upload the jpeg on server.

           

          If you also wand to upload other file formats .png... you need to scan for their specific headers.

           

          C

          • 3. Re: How to know the content type of the file Reference
            Maximist Level 1

            But what should i check for jpg files and what for png files??.. Where can i check info about??.. I found this code around to check if a byteArray is a valid jpg file..

             

             

            //get the last two bytes

            var secondToLastByte:uint = bytes.readUnsignedByte();

            var lastByte:uint = bytes.readUnsignedByte();

             

            //if the last two bytes don’t match the EOI segment header at the end of the file

            //then we know it is invalid

             

            if((secondToLastByte == 255 && lastByte == 217) || (secondToLastByte == 217 && lastByte == 0))

            {

                // valid jpg file

            }

             

            Is this code good.. ??... How can i know what to check for jpg, png or other files??

             

            Thx a lot

             

            Max

            • 4. Re: How to know the content type of the file Reference
              Claudiu Ursica Level 4

              Probably fisr thing would be to check for size to be at least the size required for  the header.

               

               

              I don't know the jpeg specification by heart. You need to go to the specification document.

              something like this or other:

              http://www.fileformat.info/format/jpeg/egff.htm

               

               

              The you will see the header structure.

               

              Read the header bytes make sure there are enough nytes for the header if not discard.

              Check for all the information in the headers to be there if they are good if they not discard.

              .....

               

              I never done it so I don't know if the code you found is enough or not. Just told you what I would do.

               

              Also never said thi way is straight forward, you need to do you reasearch and implement it.

               

               

              Also look into the JPEGEncoder class in the flex framework the encode the header there.

               

              You can draw inspiration from that logic.

               

               

              If there is other faster or easier way, I would be happy to be corrected.

              C

              • 5. Re: How to know the content type of the file Reference
                Maximist Level 1

                Because of your advices i can now begin to research.

                 

                Thx a lot

                 

                Max