How can I post file over Socket.

New Here ,
Nov 26, 2020 Nov 26, 2020

Copy link to clipboard

Copied

Hello all,

I'm a newbie of ExtendScript Toolkit CC.

 

A few days ago, I found out that I can use Socket in ExtendScript.

So I tried to many time for file upload to my Web Service.

But I've got failed all time then I write this post. 😉

 

First of all, Here is a source of my jsx file.

var conn = new Socket, reply = "";
conn.timeout = 120;

if(conn.open("localhost:80", "BINARY") && conn.connected) {
    boundary = Math.random().toString().substr(2);
    start_boundary = "--------thisisboundary" + boundary;
    boundary = "--" + start_boundary;

    textc = boundary + "\r\n"
    + "Content-Disposition: form-data; name=\"whoami\"\r\n\r\n"
    + "I don't know.";

    content = boundary + "\r\n"
    + "Content-Disposition: form-data; name=\"file\"; filename=\"test.txt\"\r\n"
    + "Content-Type: text/plain\r\n"
    + "\r\n"
    + "TEST INLINE";

    cs = "POST /upload/ HTTP/1.0\r\n"
    + "Host: localhost:80\r\n"
    + "Content-Length: " + (textc.length + content.length) + "\r\n"
    + "Connection: keep-alive\r\n"
    + "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36\r\n"
    + "Accept-Encoding: gzip, deflate\r\n"
    + "Content-Type: multipart/form-data; boundary=" + start_boundary + "\r\n"
    + "Accept-Charset: utf-8\r\n\r\n"
    + textc + "\r\n"
    + content + "\r\n"
    + boundary + "--\r\n\r\n";

    var logfile = new File("/C/Users/Zective/Desktop/log.txt");
    logfile.open("w");
    logfile.write(cs);
    logfile.close();

    conn.writeln(cs);

    reply = conn.read();
    alert(reply);
    conn.close();
}

 

And this is server-side's source. (Python Django)

import logging
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


@csrf_exempt  # For connection of Indesign
def upload(request):
    if request.method == 'POST':
        logger.info("----------------------------------------")
        logger.info(request.POST)
        logger.info(request.FILES)
        return HttpResponse("file lens : " + str(len(request.FILES)))
    else:
        return HttpResponse("Ha!")

 

 

Finally, the result of jsx file run.

 

1) log file

POST /upload/ HTTP/1.0
Host: localhost:80
Content-Length: 279
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36
Accept-Encoding: gzip, deflate
Content-Type: multipart/form-data; boundary=--------thisisboundary16539001464844
Accept-Charset: utf-8

----------thisisboundary16539001464844
Content-Disposition: form-data; name="whoami"

I don't know.
----------thisisboundary16539001464844
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain

TEST INLINE
----------thisisboundary16539001464844--

 

2) alert

Zective_0-1606450936654.png

 

3) server log

Zective_1-1606450964615.png

 

 

How I edit jsx file for success file upload?

 

Please help~!

 

Thanks in advance.

TOPICS
Scripting

Views

40

Likes

translate

Translate

Translate

Report

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
no replies

Have something to add?

Join the conversation