4 Replies Latest reply on Apr 30, 2013 9:26 AM by Joe.Charles

    Read the content of a file (via socket)




      I need to read the content of a file (.txt, or better, .jpg) and to write it again in another file.


      I have this code (in Javascript):


          conn = new Socket;


          conn.encoding = "BINARY";


          if (conn.open ("")) {




          conn.writeln("GET variousFiles/file.jpg HTTP/1.1");


          reply = conn.read(999999);




          var file = new File("C:/APPS/New_File.txt");


          file.encoding = 'BINARY';


          var open_file = file.open("w+");




          } else { alert("Errore: " + conn2.error); }



      However my new file is always empty, because I can not read the content of the first file.


      My variable "reply" is always empty.


      Thanks and best regards.

        • 1. Re: Read the content of a file (via socket)
          Bob Stucky Adobe Employee

          First thing I would do is scrap the ES effort for ActionScript. ActionScript has all that stuff built in and ready for use.







          If that's not possible for you, check your URL ( ) in a browser to see what you get back.


          You can also leverage other folks work on HTTP in ExtendScript. This library might help (I've not used it):  http://extendables.org/


          I have a foggy memory of a bug from a number of years ago.


          I belive the case was that an ExtendScript socket set up as a server would not respond to requests from localhost. It's also possible that extendscript's socket object, as a client, might not be able to connect to localhost. I just don't recall the specifics.


          Has anyone out there successfully connected to localhost using an ES Socket?






          1 person found this helpful
          • 2. Re: Read the content of a file (via socket)
            Muppet Mark Level 5

            Have you tried writing reply to the console rather than to file to see if it's empty…? Also I didn't check but shouldn't…


            var open_file = file.open("w+");


            just be…


            var open_file = file.open('w'); // without the plus

            1 person found this helpful
            • 3. Re: Read the content of a file (via socket)
              Corwin86 Level 1

              Hi Bob and Muppet,


              this below is my new code:


                   conn2 = new Socket;


                  if (conn2.open ("", "BINARY")) {




                  conn2.write("GET /Temp1/file.jpg HTTP/1.0\n\n");


                  reply2 = conn2.read(999999);




                  } else { alert("Error: " + conn2.error); }


              This seems to work perfectly, I managed to read the jpg file.



              • 4. Re: Read the content of a file (via socket)

                There is a particular case with sugested function, if content is generated on the fly by the server -php, cgi, etc.-


                In this case, http server doesn´t knows total size of response data, so response shows a chunked transfer-encoding header and data is parted in chunks, each one starting with it´s size, and following the data.


                HTTP/1.1 200 OK

                Date: Tue, 30 Apr 2013 12:43:41 GMT

                Server: Apache/1.3.31 (Win32) mod_fastcgi/2.4.1

                Connection: close

                Transfer-Encoding: chunked

                Content-Type: image/jpeg



                ÿØÿà  JFIF


                I´m using this function to decode parted response, with initial chunkedString with firs chunk size; in this case, f49:



                function getPartedBody(partedBodyString) {

                    var separatorPosition=partedBodyString.indexOf(PartsSeparator);

                    var chunkSize=new Number("0x"+partedBodyString.substring(0,separatorPosition));


                    var bodyString=new String("BINARY");


                    if (chunkSize>0)


                            getChunkedResponse(partedBodyString.substring(separatorPosition+chunkSize+PartsSeparator. length));

                    return bodyString;



                Hope this helps