Ich bin auf der Suche nach einem grundlegenden Code Beispiele zum Hochladen von Dateien auf Server mit HTTP POST-Methode auf Qt.Hochladen von Datei mit POST-Methode auf Qt4
Meine Aufgabe: Ich habe ein einfaches Qt-Programm und ich muss eine Bilddatei vom lokalen Host auswählen und auf den Server hochladen. Der Auswahlteil und die GUI sind einfach und ich habe es schon gemacht, aber mit POST-Upload bin ich verwirrt. Außerdem muss ich sagen, dass es keine Berechtigung zum Hochladen von Dateien gibt.
Wenn schon jemand dieses Thema sucht?
PS: der Grund, warum ich frage und nicht selbst programmieren ist Zeit, ich muss diese Methode schnell bekommen.
Danke, alle Erfolgslösungen von meiner Seite werden hier für andere veröffentlicht.
HINZUGEFÜGT: Hier ist mein Code, der noch nicht funktioniert. Upload-Website befindet sich here.
void CDialog::on_uploadButton_clicked() {
QFileInfo fileInfo(absPathLineEdit->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->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->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("[email protected]\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->readAll());
totalBytes.append("\r\n");
totalBytes.append("--AaB03x--");
header.setContentLength(totalBytes.length());
httpRequestAborted = false;
httpGetId = http->request(header, totalBytes);
file->close();
}
und Antwort lesen Funktion unten:
void CDialog::httpRequestFinished(int requestId, bool error) {
if (requestId != httpGetId)
return;
if (httpRequestAborted) {
if (file) {
file->close();
// file->remove();
// delete file;
file = 0;
}
return;
}
if (requestId != httpGetId)
return;
file->close();
if (error) {
// file->remove();
QMessageBox::information(this, tr("HTTP"),
tr("Download failed: %1.")
.arg(http->errorString()));
} else {
QByteArray data = http->readAll();
QFile *dataFile = new QFile("answer.txt");
dataFile->open(QIODevice::WriteOnly | QIODevice::Text);
dataFile->write(data);
dataFile->flush();
dataFile->close();
}
// delete file;
file = 0;
}
Großartig! Konnte problemlos für das Hochladen in Flask-basierte App übernommen werden –