Note that there are some explanatory texts on larger screens.

plurals
  1. POUpload file with POST method on Qt4
    primarykey
    data
    text
    <p>I'm looking for a basic code samples of how to upload files to server with HTTP POST method on Qt.</p> <p>My <strong>task</strong>: I have simple Qt program and I need to select any image file from the local host and upload it to the server. The selection part and GUI is simple and I have already done it, but with POST uploading I'm confused. In addition I have to say, that there is no authorization to upload file.</p> <p>If someone already looking this topic?</p> <p>PS: the reason why I'm asking and not coding my self is time, I need to get this method quick.</p> <p>Thank you, all success solutions from my side will be posted here for others.</p> <p><strong>ADDED:</strong> Here is my code, that doesn't work yet. Upload site located <a href="http://data.cod.ru" rel="noreferrer">here</a>.</p> <pre><code>void CDialog::on_uploadButton_clicked() { QFileInfo fileInfo(absPathLineEdit-&gt;text()); if (!fileInfo.exists()) { QMessageBox::information(this, tr("Information"), tr("File doesn't exists! Please, select another image.")); return; } file = new QFile(fileInfo.filePath()); if (!file-&gt;open(QIODevice::ReadOnly)) { QMessageBox::information(this, tr("Information"), tr("Unable to open file for reading!")); return; } QString host = "http://data.cod.ru"; QUrl url(host); QHttp::ConnectionMode mode = QHttp::ConnectionModeHttp; http-&gt;setHost(url.host(), mode, (url.port() == -1) ? 80 : url.port()); QHttpRequestHeader header("POST", "/", 1, 1); header.setValue("Host", "data.cod.ru"); header.setValue("Content-type", "multipart/form-data, boundary=AaB03x"); header.setValue("Cache-Control", "no-cache"); header.setValue("Accept", "*/*"); QByteArray bytes(fileInfo.filePath().toUtf8()); QByteArray totalBytes; totalBytes.append("--AaB03x\r\n"); totalBytes.append("Content-Disposition: form-data; name=\"email\"\r\n"); totalBytes.append("\r\n"); totalBytes.append("billgates@microsoft.com\r\n"); totalBytes.append("--AaB03x\r\n"); totalBytes.append("Content-Disposition: form-data; name=\"photo\"; filename=\"" + bytes+ "\"\r\n"); totalBytes.append("Content-Transfer-Encoding: binary\r\n\r\n"); totalBytes.append(file-&gt;readAll()); totalBytes.append("\r\n"); totalBytes.append("--AaB03x--"); header.setContentLength(totalBytes.length()); httpRequestAborted = false; httpGetId = http-&gt;request(header, totalBytes); file-&gt;close(); } </code></pre> <p>and read answer function below:</p> <pre><code>void CDialog::httpRequestFinished(int requestId, bool error) { if (requestId != httpGetId) return; if (httpRequestAborted) { if (file) { file-&gt;close(); // file-&gt;remove(); // delete file; file = 0; } return; } if (requestId != httpGetId) return; file-&gt;close(); if (error) { // file-&gt;remove(); QMessageBox::information(this, tr("HTTP"), tr("Download failed: %1.") .arg(http-&gt;errorString())); } else { QByteArray data = http-&gt;readAll(); QFile *dataFile = new QFile("answer.txt"); dataFile-&gt;open(QIODevice::WriteOnly | QIODevice::Text); dataFile-&gt;write(data); dataFile-&gt;flush(); dataFile-&gt;close(); } // delete file; file = 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload