Skip to main content
Known Participant
November 2, 2015
Answered

How to upload an html form containing a pdf file from an extension written in JavaScript to a URL?

  • November 2, 2015
  • 2 replies
  • 2194 views

I need to send a form from an Adobe Illustrator extension to a URL.

Previously we were using C++ to write the plugin with same functionality for InDesign.

To implement it as an extension for Photoshop and Illustrator we used HTML5 for UI, JQuery, AngularJS to implement functionalities of the extension.

Now I need to send a PDF to a URL in a form of HTML FORM.

We were using cURL for C++ for this purpose. What can we use with JS to do the same here?

I tried to use Ajax but it turned out that you cannot make a cross-domain call with it, and my extension doesn't have a domain name or IP or anything. Its just an extension running with Photoshop and Illustrator and we need to upload a form (containing a PDF) to a URL.

How can I do it?

Any help will be appreciated. I have added the C++ code for this function below.

bool WPConnection::UploadPDFToServer( const char* pdfPath, const char* urlInfo )
{

    bool16 uploadStatus = false;

    try // Abhishek

    {

   // Get the URL
   char connectionURL[256] = {0};

   // Now update the project panel tree
   const char* userName = g_WPDetailsVector.at(0).c_str();
   const char* passWord = g_WPDetailsVector.at(1).c_str();
   const char* projectID = g_WPDetailsVector.at(2).c_str();
   const char* statusID = g_WPDetailsVector.at(3).c_str();
   const char* pageNumber = g_WPDetailsVector.at(4).c_str();

   strcpy(connectionURL, urlInfo);
   strcat(connectionURL, "/indd_filetransfer.asp?");
   strcat(connectionURL, "username=");
   strcat(connectionURL, userName);
   strcat(connectionURL, "&password=");
   strcat(connectionURL, passWord);
   strcat(connectionURL, "&project_id=");
   strcat(connectionURL, projectID);
   strcat(connectionURL, "&status_id=");
   strcat(connectionURL, statusID);
   strcat(connectionURL, "&item_numbers=");
   strcat(connectionURL, pageNumber);

  
   CURLcode result = CURLE_FAILED_INIT;
  
  
   struct curl_httppost *formpost=NULL;
   struct curl_httppost *lastptr=NULL;
  
  
   curl_formadd(&formpost,
   &lastptr,
   CURLFORM_COPYNAME, "file",
   CURLFORM_FILE, pdfPath,
   CURLFORM_CONTENTTYPE, "pdf",
   CURLFORM_END);

   if(fCurl)
   {
        // Clearing all the connection info
   this->ClearConnectionInfo();
      
        //set options for a curl easy handle
   curl_easy_setopt(fCurl, CURLOPT_URL, connectionURL);
   curl_easy_setopt(fCurl, CURLOPT_HTTPPOST, formpost);
      
   result = curl_easy_setopt (fCurl, CURLOPT_NOPROGRESS, FALSE);
   result = curl_easy_setopt (fCurl, CURLOPT_PROGRESSFUNCTION, progress_callback);
   //Don't verify the peer.
   curl_easy_setopt(fCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
  
      
      
        //Use proxy information
   if( g_UseProxy == kTrue )
   {
   curl_easy_setopt( fCurl, CURLOPT_PROXY, g_WPDetailsVector.at(5).c_str() );
   if( g_ProxyAuthentication == kTrue )
   {
   curl_easy_setopt( fCurl, CURLOPT_PROXYUSERPWD, g_WPDetailsVector.at(6).c_str() );
   }
   }

   result = curl_easy_perform(fCurl);
   this->GetConnectionInfo(fCurl);

   curl_formfree(formpost);
   }

   if( (result == CURLE_OK) && (fHttpStatus == 200) /*&& (fResponseStatus == -1)*/)
   {
   g_UploadPercent = 100;
   uploadStatus = true;
   }
   else
   {
   g_UploadError = true;
   }

    }

    catch(...)

    {

   g_UploadError = true;

    }

    return uploadStatus;

}
This topic has been closed for replies.
Correct answer hemantk12546598

try this

var arg = 'file=@path_to_file';

var url ="your_server_url ";

window.cep.process.createProcess('/usr/bin/curl','--form',arg,url);

2 replies

hemantk12546598Correct answer
Participant
November 19, 2015

try this

var arg = 'file=@path_to_file';

var url ="your_server_url ";

window.cep.process.createProcess('/usr/bin/curl','--form',arg,url);

Inspiring
November 2, 2015

Can't you just use a standard html form with your upload page as the action?

<form method="POST" enctype="multipart/form-data" action="http://www.mysite.com/upload.php">

<input name="pdf_file" type="file" />

<input type="submit" />

</form>

RJ9191Author
Known Participant
November 16, 2015

Thanks for response. Yes I tried it and it worked just fine. But my requirement is not to browse and manually upload. I want to user to save Illustrator file and click upload and want my code to grab the path of the save document and put it in a FORM and HTTP POST it to the URL

As this tutorial Adobe CEP APIs | Adobe Developer Connection has mentioned I tried using CURL process but I didn't work.

Inspiring
November 16, 2015

For security reasons, using just standard html/html5 and Javascript, you can only upload files selected by the user otherwise you could write web pages that could upload arbitrary files from a user's machine without them knowing.

The file I/O CEP extensions on the link you gave mentions a readFile function (did you mean CEP when you said CURL?). Did you try using this?