使用c++和Qt下载二进制文件
Downloading a binary file using c++ and Qt
我想从php脚本下载一个*.exe文件并执行它。
下载文件后,我就可以再执行它了。当我查看文件内部时,里面有很多问号
PHP脚本:
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();
回复是一个"QNetworkReply"
问题是将二进制数据视为文本。
使用QDataStream::operator<<
时,来自replystring
的数据将像字符串一样处理。但它不是文本字符串,只是一系列字节。
使用QNetworkReply::read
和QFile::write
:
char buffer[2048];
qint64 size = reply->read(buffer, sizeof(buffer));
offline_ip_adress_calculator.write(buffer, size);
这里有更清晰纯粹的Qt解决方案:
QByteArray downloadedData = reply->readAll();
QFile file("somefile");
file.open(QIODevice::ReadWrite);
file.write(downloadedData.data(),downloadedData.size());
file.close();
我尝试过@someProgrammaberDude的解决方案。我用这种方式下载了一个png文件,只得到了图像的上半部分,并不奇怪,文件大小正好是2048或我设置的任何数字。
相关文章:
- 正在读取二进制文件(is_open)
- 在C++中将类(带有Vector成员)保存为二进制文件
- 如何从二进制文件中读取字符串
- 保存/加载大量短数组到二进制文件
- 从二进制文件中读取整数数组
- Android 在编译二进制文件时重建静态库
- 在 C++ 中将双精度变量写入二进制文件
- clang 的 libFuzzer 可以在同一二进制文件中测试超过 1 个 API 吗?
- C++:实际上不是从二进制文件中读取
- 如何从二进制文件中的给定符号中获取调用程序图
- 将内部带有矢量的结构保存/读取到二进制文件中
- 编译多个C++文件.调用二进制文件以运行代码
- 如何使用位字段将数据从二进制文件复制到结构中?
- uint8_t同一二进制文件的不同十进制值
- C++单个生成文件多个二进制文件
- 尝试将数字写入二进制文件时引发异常
- 在哪里可以下载适用于 Windows 的 GCC 4.3.2 二进制文件
- 使用c++和Qt下载二进制文件
- 从http下载c++二进制文件
- 使用Wininet下载二进制文件