0

I am trying to upload an executable file to the server using this code:

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

    }

the path of file is the content of variable fileNames and i extract the filename from the path with fileNames.at(0). How I can upload the file.. I am trying with this code but file is not uploaded:

 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);

... Any help would be appreciated..

2 Answers2

0

Read Uploading a file using post() method of QNetworkAccessManager

Basically you need to create the data object to be posted on the heap not on the stack! Try creating your QByteArray on the heap and follow the answer to make it go away when request is finished.

Community
  • 1
  • 1
O.C.
  • 6,711
  • 1
  • 25
  • 26
0

I have read the above thread,and i am trying with this piece of code but the problem remains..uploading fails:

 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']}")
  • And what about bigger files. You're reading... let's say 400MB file into memory at once. You can simply pass QIODevice to post, so QNetworkAccessManager will read it when it needs to put more data into socket. – Kamil Klimek May 16 '11 at 12:00