0

I have sample code for doing this in C#, Windows Phone 7, Silverlight, and I'm trying to find how to do the equivalent in C++ Qt (I am working in the Blackberry 10 Beta 3 SDK). Can I use QNetworkAccessManager? Or is there something better for this? I would appreciated some help on how to do this and some sample code if you can. Here is the sample C# code:

var request = new RestRequest(url, Method.POST);

request.AddParamater("example", "example");
request.AddParamater("example2", "example2");

request.AddFile("file", fileData, filename, "image/pjpeg");

RestSharp.RestClient restClient = new RestClient();
restClient.ExecuteAsync(request, (response) =>
{
   if (response.StatusCode == HttpStatusCode.OK)
   {
       //upload successful
   }
   else
   {
       //error occured durring upload
   }
});

Here is something I have tried:

// postVars is a QByteArray that contains a query string of the arguments 
// being passed in then I pass it in to my prepareFileUploadRequest file, 
// which appends the data for   uploading a file.

void Utils::prepareFileUploadRequest(QMap<QString, QString> *d,
        QNetworkRequest &request, QByteArray &postVars, QString path,
        QString scriptName, QString filename,
        QString stockNum)
    {

        request.setUrl(QUrl(Utils::ApiUrl));

        postVars.append("Content-type: multipart/form-data, ");
            postVars.append("boundary='---';\r\n\r\n");
        postVars.append("---\r\n");
        postVars.append("Content-Disposition: form-data; name='file' );
            postVars.append("filename='examplePic.jpg'\r\n");
        postVars.append("Content-Type: image/jpg\r\n");
        postVars.append("Content-Transfer-Encoding: binary\r\n\r\n");

        QFile file(path);
        if (!file.open(QIODevice::ReadOnly)) {
            return;
        }

        postVars.append(file.readAll() + "\r\n");
        postVars.append('---');

        request.
                setRawHeader(QString("Content-Type").toAscii(),                   
                QString("multipart/form-data; boundary='---'").toAscii());

            request.
                setRawHeader(QString("Content-Length").toAscii(),            
                QString::number(postVars.length()).toAscii());

            QNetworkAccessManager *nam = new QNetworkAccessManager(this);
            connect(nam, SIGNAL(finished(QNetworkReply *)), 
                    this, SLOT(handleUpload(QNetworkReply *)));
            nam->post(request, postVars);
 }

I call that function above, and then I get an xml reply - which says I don't have permission - and it doesn't upload any file - so that's probably an issue with the php webservice that is used, but it makes it hard for me to tell if I have taken the right approach, or if I am doing anything wrong. }

NG_
  • 6,895
  • 7
  • 45
  • 67
user1296259
  • 521
  • 1
  • 13
  • 33
  • 1
    What have you tried so far? Just look at the man page for QNetworkAccessManager, it has a post method. And then google for "upload files using qt". It's all there. – Zane Dec 05 '12 at 19:42
  • I added something I have now tried, using code from what you told me to google. – user1296259 Dec 05 '12 at 22:15
  • Good! So you're testing your program against a webservice on a different machine? For testing, it would be a good start to set up your on web service (using Apache), and test against that. Setting up Apache is easy! – Zane Dec 06 '12 at 17:22

1 Answers1

0
QString body = "" ;

const QUrl url("http://someurl.to/make/the/post" );
 QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
 QHttpPart textPartData;
 body = "" ;
 QTextStream(&body) << " this is your text to post with the file";
 textPartData.setHeader(QNetworkRequest::ContentTypeHeader, "application/json; charset=utf-8");
 textPartData.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"data\""));
 textPartData.setBody(body.toAscii());

     for(int i=0; i< number_of_files_to_send ; i++){
             QHttpPart imagePart;
             QString header_body = "";
             QTextStream(&header_body) << "form-data; name=\"" << name_of_the_file << "\"; filename=\"" << name_of_the_file << ".jpg\"" ;
             imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(header_body));
             imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));

             //prepare file for upload
             QString filepath;
             QTextStream(&filepath) << FunkyPigeonBlackberry::getInstance()->dataModelProductDetails()->custom_image(i).value("cut_image").toString();

             // pack the new file
             QFile *file = new QFile(filepath);
             file->open(QIODevice::ReadOnly);
             imagePart.setBodyDevice(file);
             file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
             multiPart->append(imagePart);
     }

 multiPart->append(textPartData);
 multiPart->append(textPartProductDetails);
 multiPart->append(textPartDataDelivery);


 QNetworkRequest request(url);

 QNetworkReply* reply = m_networkAccessManager->post(request, multiPart);
 connect(reply, SIGNAL(finished()), this, SLOT(onNetworkResponse()));
BelmonduS
  • 104
  • 3