douzhuang2570 2013-08-01 13:10
浏览 166
已采纳

使用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"

  • 写回答

2条回答 默认 最新

  • douju1280 2013-08-01 13:24
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 如何用js写一个游戏云存档
  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥30 C++行情软件的tick数据如何高效的合成K线