title: VBA/Corel Draw: How to send binary and text file in HTTP POST request to server from VBA/VB6 script running from Corel Draw 12/X4?
body: <p>I want to send Corel Draw .CDR drawing binary files and XML SVG files from the application to a server via HTTP POST.</p>
  
  <p>I have done some research and this existing post seems closest but doesn't work for my situation:
  <a href="https://stackoverflow.com/questions/158633/how-can-i-send-an-http-post-request-to-a-server-from-excel-using-vba">How can I send an HTTP POST request to a server from Excel using VBA?</a></p>
  
  <p>I've added a user-custom button to the Corel Draw tool pane and created a macro to run when this button is pressed. The macro contains the following code.</p>
  
  <pre><code>
  
  Sub OpenLabelPrintExport()
      '
      ' Recorded 24/06/2008
      '
      ' Description:
      '
      '
  
  ' Add a reference to Microsoft WinHTTP Services
  Const HTTPREQUEST_SETCREDENTIALS_FOR_SERVER = 0
  
      'MsgBox "hello"
  
      Dim expflt As ExportFilter
      Dim expopt As StructExportOptions
      Dim responseText As String
      Set expopt = New StructExportOptions
      expopt.UseColorProfile = False
      ' expopt.DontExportFonts
      Set expflt = ActiveDocument.ExportEx("C:\afile.svg", cdrSVG, cdrAllPages, expopt)
      expflt.Finish
  
      file = "C:\afile.svg"
  
      Dim oS As ADODB.STREAM
      Set oS = New STREAM
  
      oS.Type = 2
      oS.Open
      oS.LoadFromFile file
  
      Dim contentlength As Integer
      contentlength = oS.Size
  
  sEntityBody = "-----boundary" & vbCrLf
  sEntityBody = sEntityBody & "Content-Dispostion: form-data; name=fileInputElementName; filename=""" + sFileName + """" & vbCrLf
  sEntityBody = sEntityBody & "Content-Transfer-Encoding: 7bit" & vbCrLf
  sEntityBody = sEntityBody & "Content-Type: text/xml" & vbCrLf & vbCrLf
  ' did use oS
  sEntityBody = sEntityBody & "text" & vbCrLf
  sEntityBody = sEntityBody & "-----boundary--" & vbCrLf & vbCrLf
  
  ' Set xhr = New MSXML2.XMLHTTP30
  
  Dim xhr As WinHttp.WinHttpRequest
  Set xhr = New WinHttpRequest
  
  xhr.Open "POST", sUrl, False
  xhr.SetRequestHeader "Content-Type", "multipart/form-data; boundary=""-----boundary"""
  xhr.Send sEntityBody
  
  End Sub
  
  </code></pre>
  
  <p>On my server, I have the following Perl CGI script to accept the file:</p>
  
  <pre><code>
  #!/usr/bin/perl -wT
  
  use strict;
  use CGI;
  use CGI::Carp qw ( fatalsToBrowser );
  use File::Basename;
  
  $CGI::POST_MAX = 1024 * 5000;
  my $safe_filename_characters = "a-zA-Z0-9_.-";
  my $upload_dir = "/usr/lib/cgi-bin/";
  
  my $query = new CGI;
  my $filename = $query->param("file");
  my $email_address = $query->param("email_address");
  
  if ( !$filename )
  {
   print $query->header ( );
   print "There was a problem uploading your file (try a smaller file).";
   exit;
  }
  
  my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' );
  $filename = $name . $extension;
  $filename =~ tr/ /_/;
  $filename =~ s/[^$safe_filename_characters]//g;
  
  if ( $filename =~ /^([$safe_filename_characters]+)$/ )
  {
   $filename = $1;
  }
  else
  {
   die "Filename contains invalid characters";
  }
  
  my $upload_filehandle = $query->upload("file");
  
  open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";
  binmode UPLOADFILE;
  
  while (  )
  {
   print UPLOADFILE;
  }
  
  close UPLOADFILE;
  
  print STDOUT "success";
  
  </code></pre>
  
  <p>I have tested the server-side script with a HTML form on a brower.</p>
  
  <p>I would like advise on getting the VBA script that runs in Corel Draw to work correctly. I have searched and searched and can't seem to find a definitive answer to sending binary and text files from a VBA enabled application to a server via HTTP POST. I have bought some books on the subject too but am no wiser.</p>
  
  <p>I need this to work with Corel Draw 12 and Corel Draw X4.</p>
  
  <p>Thanks in advance.</p>