Solved Can't download xls or xlsx files
-
Hi
I try to download an Excel-file, but I always get the following error:
"QZip: invalid header signature, index may be incomplete".When I download the file via the browser, everything is okay; I can open the excel-file.
When I try to download the file via my application, Excel tells me that the file format or file extension is not valid. To load the downloaded file with QXlsx does not work either.QString downloadaddress ="https://file-examples.com/wp-content/uploads/2017/02/file_example_XLSX_50.xlsx"; QNetworkAccessManager *manager = new QNetworkAccessManager(this); connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*))); const QUrl url = QUrl(file_to_download); QNetworkRequest request; request.setUrl(url); QSslConfiguration configSsl = QSslConfiguration::defaultConfiguration(); configSsl.setProtocol(QSsl::AnyProtocol); request.setSslConfiguration(configSsl); manager->get(request); void MainWindow::replyFinished (QNetworkReply *reply) { qDebug() << "read rawHeaderList:" <<reply->rawHeaderList(); qDebug() << "read contentheadertype:" << reply->header(QNetworkRequest::ContentTypeHeader).toString(); qDebug() << "read serverheader:" << reply->header(QNetworkRequest::ServerHeader).toString(); QString filename = "C:/downloaded.xlsx"; QFile *file = new QFile(filename); if(!file->open(QIODevice::ReadWrite)) { qDebug() << "Error"; } else { QTextStream outStream(file); outStream << reply->readAll(); file->close(); } QFile excelfile(excel_file); if (!excelfile.exists()) { qDebug() << "file does not exist"; } else { if (excelfile.isReadable()) {qDebug() << "is readable";} else { qDebug() << "ist NOT readable";} }
reply->header(QNetworkRequest::ContentTypeHeader).toString() is: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
qDebug tells me that the file is NOT readable and then it gives me the message: "QZip: invalid header signature, index may be incomplete"
What is wrong?
Any help would be appreciated.Thanks
-
Thanks,
file->write(reply->readAll())
really worked.
Obviously it was a problem of QFile.The error message "QZip: invalid header signature, index may be incomplete" came from QXlsx.
-
Hi and welcome to devent forum
Did you check if the file is there where you expect?
Possibly an AV scanner is preventing you from access. -
Hi,
Yes, the file is where it should be. There is some downloading, but to my mind, the download process seems to be incomplete. When I try to open the Excel-file with Excel, it tells me that the "file format or file extension is not valid".
Qt tells me "QZip: invalid header signature, index may be incomplete" when I try to open the file with my qt-application.I turned off the AV-scanner and got the same result.
-
You are using QTextStream which is probably causing some translations in the byte stream. Try QDataStream since it is a binary dataset you are downloading.
-
Why not simply using QFile::write(reply->readAll()) instead a QTextStream or QDataStream?
-
Thanks,
file->write(reply->readAll())
really worked.
Obviously it was a problem of QFile.The error message "QZip: invalid header signature, index may be incomplete" came from QXlsx.
-
@mark58 said in Can't download xls or xlsx files:
really worked.
So if your issue is solved, please don't forget to mark your post as such. Thanks.