QNetworkAccessManager:将可执行文件上传到apache服务器

时间:2011-05-11 21:06:46

标签: qt4

我正在尝试使用以下代码将可执行文件上传到服务器:

    QFileDialog dialog(this);
    dialog.setDirectory(QDir::home());
    QStringList fileNames = QFileDialog::getOpenFileNames(this,tr("Open File"));
    if(fileNames.count() == 1)
    {
        QFileInfo fi(fileNames.at(0));

    }

file的路径是变量fileNames的内容,我使用fileNames.at(0)从路径中提取文件名。 我如何上传文件.. 我正在尝试使用此代码,但文件未上传:

 QByteArray postData;

 postData.append(path);

 QString url ="http://localhost/upload.php";
 //type header
 req.setHeader(QNetworkRequest::ContentTypeHeader,"multipart/form-data");
 req.setUrl(url);

 manager=new QNetworkAccessManager(this);
 reply=manager->post(req,postData);

... 任何帮助将不胜感激..

2 个答案:

答案 0 :(得分:0)

阅读Uploading a file using post() method of QNetworkAccessManager

基本上你需要创建要在堆上发布而不在堆栈上的数据对象!尝试在堆上创建QByteArray并按照答案让它在请求完成时消失。

答案 1 :(得分:0)

我已经阅读了上面的帖子,我正在尝试使用这段代码,但问题仍然存在..上传失败:

 file=new QFile(path);
 file->open(QIODevice::ReadOnly);
 postData.append(file->readAll());

 QString url ="http://localhost/uploadFile.php";
 //type header
 req.setHeader(QNetworkRequest::ContentTypeHeader,"application/octet-stream");

 req.setUrl(url);

 manager=new QNetworkAccessManager(this);


 reply=manager->post(req,postData);
 connect(manager,SIGNAL(finished(QNetworkReply*)),this,     SLOT(replyFinished(QNetworkReply*)));

}

 //////php file///////

// The software package name
 $fileName = trim($_POST['fileName']);

// Make directory
mkdir("files/$fileName");

// Upload file
move_uploaded_file ($_FILES['upload'] ['temp_name'],  "files/$fileName/{$_FILES['uploadFile'] ['name']}")