gpt4 book ai didi

qt - 使用Qt QNetworkRequest上传文件

转载 作者:行者123 更新时间:2023-12-04 04:35:20 24 4
gpt4 key购买 nike

我在尝试使用QNetworkRequest将文件上传到服务器时遇到了一些问题。我一直将这个链接(http://qt-project.org/forums/viewthread/11361)用作模板,但是仍然收到POST错误(具体为203)。到目前为止,这就是我所拥有的。

void MainWindow::processFile(){

QByteArray postData;
//Look below for buildUploadString() function
postData = mReport->buildUploadString();

QUrl mResultsURL = QUrl("http://" + VariableManager::getInstance()->getServerIP() + "/uploadFile.php");
QNetworkAccessManager* mNetworkManager = new QNetworkAccessManager(this);


QString bound="margin"; //name of the boundary

QNetworkRequest request(mResultsURL); //our server with php-script
request.setRawHeader(QString("Content-Type").toAscii(),QString("multipart/form-data; boundary=" + bound).toAscii());
request.setRawHeader(QString("Content-Length").toAscii(), QString::number(postData.length()).toAscii());


connect(mNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(printScriptReply(QNetworkReply*))); //This slot is used to debug the output of the server script
mNetworkManager->post(request,postData);
}


QByteArray ReportParser::buildUploadString()
{
QString path = VariableManager::getInstance()->getReportDirectory();
path.append("\\\\");
path.append(getReportFileName());


QString bound="margin";
QByteArray data(QString("--" + bound + "\r\n").toAscii());
data.append("Content-Disposition: form-data; name=\"action\"\r\n\r\n");
data.append("uploadFile.php\r\n");
data.append(QString("--" + bound + "\r\n").toAscii());
data.append("Content-Disposition: form-data; name=\"uploaded\"; filename=\"");
data.append(getReportFileName());
data.append("\"\r\n");
data.append("Content-Type: text/xml\r\n\r\n"); //data type

QFile file(path);
if (!file.open(QIODevice::ReadOnly)){
qDebug() << "QFile Error: File not found!";
return data;
} else { qDebug() << "File found, proceed as planned"; }

data.append(file.readAll());
data.append("\r\n");
data.append("--" + bound + "--\r\n"); //closing boundary according to rfc 1867


file.close();

return data;
}

这是服务器上用于处理文件的脚本:
<?php

$uploaded_type = $_FILES['uploaded']['type'];

$target = "/var/www/webpage/results/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;

echo "target: ";
echo $target;

//This is our limit file type condition
if ($uploaded_type =="text/xml"){
echo "We have an xml file!\r\n";
}

//Here we check that $ok was not set to 0 by an error
//If everything is ok we try to upload it
if ($ok==0){

echo "Sorry your file was not uploaded";

} else {

echo "Looking good!";

if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)){
echo "The file successfully ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} else {
echo "Sorry, there was a problem uploading your file.";
}
}
?>

我知道该脚本有效,因为使用基本HTML表单时它将正确处理文件。但是,事情的Qt端总是返回POST错误。

最佳答案

问题是网络上的数据包丢失。上面的代码实际上有效。

关于qt - 使用Qt QNetworkRequest上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16735595/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com