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 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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']}")

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.