1 Reply Latest reply on Jun 7, 2010 5:51 AM by RorohikoKris-u5pUJw

    Download an image with JS?

    Pontus Uggla Level 1

      I´m trying to download an image from an URL with Javascript using socket.

       

      My first problem was separating the header from the body. I do it by reading line by line and if the line starts with "\n" then it seams to be the body.

       

      A problem with this is that it´s very very slow. Reading a 1x1 pixel png takes 5-10 seconds.

       

      The stranger thing is that I can´t write the content to file. I can write a string to a file, but not the body of the image, toString() doesn´t seem to help.

       

      Anyone with experience or advice?

       

      var img_data = "";

      var line;

      var body_start = false;

      var conn = new Socket;

       

      if (conn.open ("www.test.com:80", "BINARY")) {

      var request = "GET /phoenix/images/5_topnavlogo.jpg HTTP/1.1\n" +

        "Host: www.test.com\n" +

        "User-Agent: InDesign ExtendScript\n" +

        "Accept: text/xml,text/*,*/*\n" +

        "Accept-Encoding:\n" +

        "Content-Length: 0\r\n" +

        "Content-Type: application/x-www-form-urlencoded\r\n" +

        "Connection: keep-alive\n" +

        "Accept-Language: *\n" +

        "Accept-Charset: utf-8\n" +

        "\n";

      conn.write(request);

       

      while(!conn.eof) {

      line = conn.readln(255);

       

      if(line.substring(0, 1) == "\n") {

      img_data = line.substring(1);

      }

      }

       

      conn.close();

       

      alert(img_data);

       

      var img_file = new File(File(app.activeScript).parent + '/test.txt');

      img_file.open('w');

      img_file.write(img_data);

      img_file.close();

       

      alert("Done");

      }