使用c ++和Qt下载二进制文件

I want to download a *.exe file from a php script and execute it.

After I download the file I can' execute it anymore. When I look inside the file there are a lot question marks in it.

PHP script:

header('Content-Description: File Transfer');
header('Content-Type: application/x-download');
header('Content-Disposition: attachment; filename='.basename($file_name));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file_name));
ob_clean();
flush();
readfile($file_name);
exit;

C++:

 QFile offline_ip_adress_calculator(QDir::currentPath() + "/offline_ip_adress_calculator.exe");

    //Check if the File exists and clear its content
    if(!offline_ip_adress_calculator.open(QFile::ReadWrite | QIODevice::Truncate))
    {
        msgBox.critical(this, "I/O error", "Can't open offline_ip_adress_calculator.exe for update");
        return;
    }

    QDataStream text_stream(&offline_ip_adress_calculator);
    while(reply->size() > 0)
    {
        QByteArray replystring = reply->read(2048);
        text_stream << replystring;
    }

    offline_ip_adress_calculator.close();

reply is a "QNetworkReply"

The problem is that you treat binary data as text.

When you use QDataStream::operator<< the data from replystring is handled like a string. But it's no textual string, just a series of bytes.

Instead use QNetworkReply::read and QFile::write:

char buffer[2048];
qint64 size = reply->read(buffer, sizeof(buffer));
offline_ip_adress_calculator.write(buffer, size);

Here is more clear and pure Qt Solution:

   QByteArray downloadedData = reply->readAll();
   QFile file("somefile");
   file.open(QIODevice::ReadWrite);
   file.write(downloadedData.data(),downloadedData.size());
   file.close();

I have tried @SomeProgrammerDude's solution.I downloaded a png file that way,got only upper half of the image and Not Surprisingly the file size was exactly 2048 or whatever number I set.