[Interest] QHttpMultiPart not working.

Jason H jhihn at gmx.com
Fri Dec 12 19:27:19 CET 2014


I am trying to sendmultiple files in a multiart message. I've adapted the example, and there is never a boundary or content put out for any part.

First the code:
	QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);

	foreach (QVariant v, filenames) {
		QString filename = v.toString();

		QString shortFilename;
		if (filename.contains("/"))
			shortFilename = filename.mid(filename.lastIndexOf("/")+1);
		else
			shortFilename = filename;

		QString mimeType;
		if (filename.endsWith(".png"))
			mimeType = "image/png";
		else if (filename.endsWith(".jpg") || filename.endsWith(".jpeg"))
			mimeType = "image/jpeg";

		QFile *file = new QFile(filename);
		if (file->open(QIODevice::ReadOnly)){
			QHttpPart imagePart;
			imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(mimeType));
			imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QString("form-data; name=\"%1\"").arg(shortFilename)));
			imagePart.setBodyDevice(file);
			file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
			multiPart->append(imagePart);
			qDebug() << "appended imgePart" << filename << shortFilename;
		} else {
			qDebug() << Q_FUNC_INFO << "Cannot open file" << filename;
		}
	} // foreach
	QNetworkReply *reply =_nam.post(QNetworkRequest(url), multiPart);
	multiPart->setParent(reply);
	connect(reply, SIGNAL(uploadProgress(qint64,qint64)), this, SLOT(completed(qint64,qint64)));
	connect(&_nam, SIGNAL(finished(QNetworkReply *)), this, SLOT(completed(QNetworkReply *)));

Next, the output:
--start---
POST /files HTTP/1.1
Content-Type: multipart/form-data; boundary="boundary_.oOo._MTk2MDQ2MTg0Mg==NzYxMjMyNTgyNDQ5NDA2MDk="
MIME-Version: 1.0
Content-Length: 407
Connection: Keep-Alive
Accept-Encoding: gzip, deflate
Accept-Language: en-US,*
User-Agent: Mozilla/5.0
Host: localhost:10000

--end----

There are no parts sent.

Any idea why?

Thanks!



More information about the Interest mailing list