void MainWindow::uploadScreenshotToServer(QUrl uploadUrl){ QFile file("img.png"); if(file.open(QIODevice::ReadOnly)) { QNetworkRequest request; request.setUrl(uploadUrl); request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/x-www-form-urlencoded")); QByteArray boundary("56gfqgE8VdoO"); QByteArray data; data.append("Content-Type: multipart/form-data; boundary="); data.append(boundary); data.append("\n\n--"); data.append(boundary); data.append("\n"); data.append("Content-Disposition: form-data; name=\"photo\"; filename=\"img.png\"\n"); data.append("Content-Type: image/png\r\n"); data.append("Content-Transfer-Encoding: binary\n\n"); data.append(file.readAll()); data.append("\n--"); data.append(boundary); data.append("--\n"); reply = manager.post(request, data); connect(reply, SIGNAL(finished()), this, SLOT(uploadScreenshotToServerReplyFinished())); } file.close();}
{ QFile file("img.png"); if(file.open(QIODevice::ReadOnly)) { QNetworkRequest request; request.setUrl(uploadUrl); QHttpMultiPart multiPart(QHttpMultiPart::FormDataType); QHttpPart imagePart; imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/png")); imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"photo\"")); imagePart.setBodyDevice(&file); multiPart.append(imagePart); reply = manager.post(request, &multiPart); connect(reply, SIGNAL(finished()), this, SLOT(uploadScreenshotToServerReplyFinished())); } file.close();}
void MainWindow::uploadScreenshotToServer(QUrl uploadUrl){ QBuffer buffer(&data); buffer.open(QIODevice::ReadWrite); screenshotPixmap.save(&buffer, "png"); buffer.close(); QNetworkRequest request; request.setUrl(uploadUrl); QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType); QHttpPart imagePart; imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/png")); if(saveMethod->currentIndex() == 0) { imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"photo\"; filename=\"img.png\"")); } else if(saveMethod->currentIndex() == 1) { imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file1\"; filename=\"img.png\"")); } imagePart.setBody(data); multiPart->append(imagePart); reply = manager.post(request, multiPart); connect(reply, SIGNAL(finished()), this, SLOT(uploadScreenshotToServerReplyFinished())); connect(reply, SIGNAL(finished()), multiPart, SLOT(deleteLater()));}
imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"photo\"; filename=\"img.png\""));