[Solved] Invalid characters at starting in downloaded file



  • Hi everyone,
    I am downloading a page using QNetwork classes but getting some invalid characters on the starting of the file downloaded.
    Does anyone knows why my data is getting corrupted ??
    I tried downloading http://ashish-bansal.in webpage using this and getting this as output at starting line :

    @B�<!doctype html>@

    Full Code :
    @Download::Download(QString rawURL) : QObject()
    {
    mQnam = new QNetworkAccessManager();
    mUrl = new QUrl(rawURL);
    mTempFile = new QTemporaryFile("name"); //QTemporaryFile *
    if(!mTempFile->open()){
    return;
    }
    mReq = new QNetworkRequest();
    mReq->setUrl(*mUrl);
    mDownloadReply = mQnam->get(*mReq);

    connect(mDownloadReply, &QNetworkReply::downloadProgress, this, &Download::downloadProgress);
    connect(mQnam, &QNetworkAccessManager::finished, this, &Download::downloadFinished);
    

    }

    void Download::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
    {
    }

    void Download::downloadFinished(QNetworkReply *reply)
    {
    qDebug() << "Download Finished as whole file";
    QDataStream out(mTempFile);
    QByteArray data = mDownloadReply->readAll();
    out << data;
    tempFile->close();
    reply->deleteLater();
    }
    @


  • Moderators

    You are using QDataStream. This is a portable serialization class which means it will add some extra info e.g. the type. The extra characters are the encoded signature of QByteArray type.
    To just store the file contents either use QTextStream or simply directly the write() method of the QTemporaryFile.



  • [quote author="Chris Kawa" date="1419879651"]You are using QDataStream. This is a portable serialization class which means it will add some extra info e.g. the type. The extra characters are the encoded signature of QByteArray type.
    To just store the file contents either use QTextStream or simply directly the write() method of the QTemporaryFile.[/quote]

    Everyday I learn something new from you :)
    Thanks a lot!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.