Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Sending binary file with socket

Community Beginner ,
Mar 21, 2018 Mar 21, 2018

Hi!

I have already read all the forum posts related to this topic ... but unfortunately I can't upload a binary file through socket (js for indesign CC2017, os 10.13.3). 
The answer of server is always 400 Bad Request

I enclose the code ...

Where I'm wrong?

Thanks!

sendDataToServerLocal("~/Desktop/prova.pdf", "prova")

function sendDataToServerLocal(filepath, filename) {

    conn = new Socket;

    reply = "";

    // Read file

    var f = File(filepath);

    f.encoding = 'BINARY';

    f.open("r");

    var fContent = f.read();

    f.close();

    // Connect to server

    if (conn.open("192.168.1.125:80", "BINARY")) {

        conn.timeout = 20000;

        boundary = Math.random().toString().substr(2);

        content = "--" + boundary

         + "Content-Disposition: form-data; name=\"file\"; filename=\"" + filename + ".pdf\"\r\n"

         + "Content-Type: application/octet-stream\r\n"

         + "\r\n"

         + fContent;

        cs = "POST mb_riediting2/php/upload.php HTTP/1.0\r\n"

         + "Content-Length: " + content.length + "\r\n"

         + "Content-Type: multipart/form-data; charset=utf-8; boundary=" + boundary + "\r\n"

         + "Host: 192.168.1.125:80\r\n"

         + "User-Agent: Apache-HttpClient/4.3.1 (java 1.5)\r\n"

         + "Accept: */*\r\n"

         + "Expect: 100-continue\r\n"

         + "\r\n"

         + content

         + "--" + boundary + "--\r\n"

        conn.write(cs);

        reply = conn.read(999999);

        conn.close();

       

  alert(reply)       

       

        if (reply.indexOf("200 OK") > 0) {

            alert("File successfully uploaded!");

        } else {

            //throw new Error("Error:" + reply);

        }

    } else {

       // throw new Error("Can't connect to server");

    }

}

Schermata 2018-03-21 alle 09.23.19.png

TOPICS
Scripting
786
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Mar 22, 2018 Mar 22, 2018

Wouldn't that be stronger to use cURL ?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 26, 2020 Feb 26, 2020
LATEST

Have you find a way to do finally ?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines