title: Uploading files using Qt QNetworkRequest
body: <p>I've been having some issues trying to upload files to a server using QNetworkRequest. I've been using this link (<a href="http://qt-project.org/forums/viewthread/11361" rel="noreferrer">http://qt-project.org/forums/viewthread/11361</a>) mostly as a template, but am still getting POST errors (203 to be specific). Here is what I have so far.</p>
  
  <pre><code>void MainWindow::processFile(){
  
      QByteArray postData;
      //Look below for buildUploadString() function
      postData = mReport-&gt;buildUploadString();
  
      QUrl mResultsURL = QUrl("http://" + VariableManager::getInstance()-&gt;getServerIP() +  "/uploadFile.php");
      QNetworkAccessManager* mNetworkManager = new QNetworkAccessManager(this);
  
  
      QString bound="margin"; //name of the boundary
  
      QNetworkRequest request(mResultsURL); //our server with php-script
      request.setRawHeader(QString("Content-Type").toAscii(),QString("multipart/form-data; boundary=" + bound).toAscii());
      request.setRawHeader(QString("Content-Length").toAscii(), QString::number(postData.length()).toAscii());
  
  
      connect(mNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(printScriptReply(QNetworkReply*))); //This slot is used to debug the output of the server script
      mNetworkManager-&gt;post(request,postData);
  }
  
  
  QByteArray ReportParser::buildUploadString()
  {
      QString path = VariableManager::getInstance()-&gt;getReportDirectory();
      path.append("\\\\");
      path.append(getReportFileName());
  
  
      QString bound="margin";
      QByteArray data(QString("--" + bound + "\r\n").toAscii());
      data.append("Content-Disposition: form-data; name=\"action\"\r\n\r\n");
      data.append("uploadFile.php\r\n");   
      data.append(QString("--" + bound + "\r\n").toAscii());   
      data.append("Content-Disposition: form-data; name=\"uploaded\"; filename=\"");
      data.append(getReportFileName());
      data.append("\"\r\n");  
      data.append("Content-Type: text/xml\r\n\r\n"); //data type
  
      QFile file(path);
          if (!file.open(QIODevice::ReadOnly)){
              qDebug() &lt;&lt; "QFile Error: File not found!";
              return data;
          } else { qDebug() &lt;&lt; "File found, proceed as planned"; }
  
      data.append(file.readAll());           
      data.append("\r\n");
      data.append("--" + bound + "--\r\n");  //closing boundary according to rfc 1867
  
  
      file.close();
  
      return data;
  }
  </code></pre>
  
  <p>Here is the script on the server to process the file:</p>
  
  <pre><code>&lt;?php
  
         $uploaded_type = $_FILES['uploaded']['type'];
  
      $target = "/var/www/webpage/results/";
      $target = $target . basename( $_FILES['uploaded']['name']) ;
      $ok=1;
  
      echo "target: ";
      echo $target;
  
      //This is our limit file type condition
      if ($uploaded_type =="text/xml"){
              echo "We have an xml file!\r\n";
      }
  
      //Here we check that $ok was not set to 0 by an error
      //If everything is ok we try to upload it
      if ($ok==0){
  
              echo "Sorry your file was not uploaded";
  
      } else {
  
              echo "Looking good!";
  
              if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)){
                      echo "The file successfully ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
              } else {
                      echo "Sorry, there was a problem uploading your file.";
              }
      }
  ?&gt;
  </code></pre>
  
  <p>I know that the script works, as it will handle the file properly when using a basic HTML form. However, the Qt side of things keeps returning POST errors.</p>