Andy Shaw

Qt Commercial Support Weekly #14 - Introducing QHttpMultiPart

2/20/2012 9:41 AM  | Posted by: Andy Shaw

 

This week we have been dealing with quite a variety of different components in support. Quite a number of things have been reported as bugs and we have been supplying fixes and workarounds to those wherever we can.  Since July 2011, we have been going at a steady increase in the percentages of bugs being fixed and we are edging closer to 80% of all bugs reported to Qt Support get fixed or given a workaround from someone in the support team.  This is not to say that the other things don't get fixed at all. All bugs that are still outstanding are passed on to the R&D team so that they can look into fixing them too, as Qt Commercial is dedicated to getting these issues fixed as well as implementing new features.

 

Note: This blog is best viewed with Mozilla Firefox. Internet Explorer is giving errors on the code snippet.

 

Now that shameless marketing on my side is out of the way it is on to the latest technical nugget of information for the week.

 

With the release of Qt Commercial 4.8.0 a new network related class somewhat sneaked under the radar, which makes things easier when it comes to uploading files via a form or similar on a HTML webpage.  This class is called QHttpMultiPart and comes with another helper class called QHttpPart.  It is a relatively straight-forward class to use, in fact, as the bulk of it is done for you once you specify the file to be sent with it.  So, if you want to upload data via a form then you would do something like:

 

 

  QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType, this);


 

Then, for each element you want to pass up you would create a QHttpPart and tell it the data you want to send.  Since it takes a QIODevice you are not restricted to using a QFile. You can use your own QIODevice subclass, or even a QNetworkReply, if you are so inclined.  You can also pass the data directly.  In the code below we are going to pass an image, so we need to set the headers to  indicate that it is an image being sent.

 

 

  QHttpPart filePath;

  filePath.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/png"));

  filePath.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form.data; name=\"image\""));

  QFile *file = new QFile("image.png");

  file->open(QIODevice::ReadOnly);

  filePath.setBodyDevice(file);

 

 

Now we just tie the two together with and post it to the QNetworkRequest for our url:

 

   multiPart->append(filePath);

   QNetworkReply *reply = manager.post(request, multiPart);

 

 

You are not restricted to just one QHttpPart here. You can append as many as you want to the QHttpMultiPart and they will be sent all in the same QHttpMultiPart data packets.  

 

One thing to remember and watch out for is the QIODevice that you have set as the body device is not owned by the QHttpPart, so you need to delete it after it has been finished with.  One approach to this would be to call setParent() on the iodevices and pass the QNetworkReply as the parent.  So, when the QNetworkReply is finished with it would delete the body parts for you. 

 

 

Until next week, happy coding :)

Comments:

Jim | 2/20/2012 12:39 PM
Another thing that you should do is a Qt(C++) websockets client implementation. This will be very helpful for those who don't want to use webkit on their programs.

Peter | 2/21/2012 12:30 AM
nice article, interesting use case is also uploading images to Flickr or Picasa or uploading videos to Youtube etc.

PaceyIV | 4/18/2012 8:40 PM
I think there is a bug in this class. Look at this post at the qtcenter for more details: http://www.qtcentre.org/threads/48475-POST-Request-problems-with-IDrive-EVS-REST-APIs?p=218478#post218478

Add new comment:

User verification Image for user verification  
     

Tags

Archive

Authors

Pasi Matilainen

Pasi is a Software Specialist working at Digia, Qt Commercial R&D and he concentrates on Mac OS X development. Pasi holds an M.Sc. degree in Information Technology from the Tampere University of Technology, Finland.

Tarja Sundqvist

Tarja is a Senior Software Engineer in the Digia, Qt Commercial Support team. She has been working in Digia for over 10 years in various positions: software development, testing, error management. Now, Tarja is focusing on helping Qt Commercial customers with their daily Qt problems on Windows and Linux platforms. Tarja holds an M.Sc. degree in Information Processing Science from the University of Oulu, Finland.

Akseli Salovaara

Akseli is a Software Specialist at Digia, Qt Commercial R&D and is responsible for the Qt Commercial releases and deliveries. Akseli holds an B.Sc. degree in Information Technology from the University of Applied Sciences in Jyväskylä, Finland.

Samuli Piippo

Samuli is a Software Specialist at Digia, Qt Commercial R&D with a concentration on  embedded Linux and RTOS development. Samuli holds an M.Sc. degree in Information Processing Science from the University of Oulu, Finland.

Katherine Barrios

Katherine is the Marketing Manager at Digia, Qt Commercial. She is responsible for getting the word out about Qt Commercial to the Qt ecosystem and working together with our customers and the Qt community to further extend the love for Qt on desktop and embedded. She was previously employed at Nokia, Qt Development Frameworks as Program Marketing Manager and is based in Oslo, Norway.

Sami Makkonen

Sami is a Senior Product Manager working at Digia, Qt Commercial R&D and he is responsible for the product planning including new feature development and enhancements to existing functionality. Sami holds an M.Sc.(Econ.) degree in Computer Science.

Andy Shaw

Andy is the Head of Support at Digia, Qt Commercial and has been working with Qt and supporting customers using Qt for 11 years.  He thrives on solving customer problems and getting feedback from them.

Tuukka Turunen

Tuukka is the Director of R&D at Digia, Qt Commercial and is responsible for the planning, creation, verification and delivery of the Qt Commercial product. Tuukka holds a M.Sc.(Eng) and Licentiate of Technology degrees in Computer Science.

Qt Commercial Team