पर POST विधि के साथ फ़ाइल अपलोड करें मैं Qt पर HTTP POST विधि के साथ सर्वर पर फ़ाइलों को अपलोड करने के तरीके के मूल कोड नमूने की तलाश में हूं।QT4
मेरा कार्य: मेरे पास सरल क्यूटी प्रोग्राम है और मुझे स्थानीय होस्ट से किसी भी छवि फ़ाइल का चयन करने और सर्वर पर अपलोड करने की आवश्यकता है। चयन भाग और जीयूआई सरल है और मैंने इसे पहले ही कर लिया है, लेकिन पोस्ट अपलोड के साथ मैं उलझन में हूं। इसके अलावा मुझे कहना है कि फ़ाइल अपलोड करने के लिए कोई प्राधिकरण नहीं है।
यदि कोई पहले से ही इस विषय को देख रहा है?
पीएस: कारण मैं क्यों पूछ रहा हूं और स्वयं को कोडिंग नहीं कर रहा हूं, मुझे यह विधि जल्दी से प्राप्त करने की आवश्यकता है।
धन्यवाद, मेरी तरफ से सभी सफल समाधान दूसरों के लिए यहां पोस्ट किए जाएंगे।
जोड़ा गया: यहां मेरा कोड है, जो अभी तक काम नहीं करता है। 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();
}
और पढ़ने के जवाब नीचे समारोह:
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;
}
बढ़िया! फ्लास्क-आधारित ऐप पर अपलोड करने के लिए आसानी से अपनाया जा सकता है –