Important: Please read the Qt Code of Conduct -

Trouble understanding how to handle unsupportedContent() signal and actual saving of file

  • Hello!

    I made a little download test program to test unsupportedContent(QNetworkReply*) handling.

    The trouble for me is now how and where to handle the actual "Save File dialog" stuff ???

    This test code is for both normal file URL's like (
    and for dynamically generated files (has "Content-Disposition" header that has the filename to save in it)

    Any advise how to do this correctly and efficiently ?

    @class MainWindow : public QMainWindow
    explicit MainWindow(QWidget *parent = 0);
    public slots:
    void slot_unsupportedContent(QNetworkReply * reply);
    void slot_downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
    void slot_finished();
    Ui::MainWindow *ui;
    QString fileName;

    #include <QMessageBox>
    #include <QDebug>
    #include <QFileInfo>
    #include <QFileDialog>
    #include <QDir>

    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)

    /* To test dynamically generated file download ("Content-Disposition" header)
     * Just give that page some YouTube URL (like
     * and continue so long untill you see the Download button and see what happens */
    /* To test normal URL download, that is URL contains filename part */


    MainWindow::slot_unsupportedContent(QNetworkReply * reply)
    /* To see the download progress.
    * Downloading is actually in progress already in this point!
    * That is, reply is already downloading data */

    /* To check the final download status, and if no error then save to disk.
     * But where should the user be asked the filename to save with
     * QFileDialog::getSaveFileName() ???
     * If it's done here then the download could be finished already
     * (if very small file) and then slot_finished() called
     * before user even had chance to give filename to save!
     * (you can see the progressbar starting going immediately before savedialog)
     * If it's done at the slot_finished() then with large files
     * user has to wait untill whole download complete before he/she
     * can give filename to save.
     * Does this download have to do "twice" ? Once in here to get the
     * metadata from reply so that user can save with correct filename
     * and then at the end of this slot_unsupportedContent()
     * call download method or emit some signal to start download method ?
     * Sounds wastefull of bandwidth...
     * Isn't there any way to postpone the actual coming of data from reply
     * object untill the metadata for filename has been gotted ??? */
    /* Diagnostic purposes only. Especially to see if server sends
     * "Content-Disposition" header when it send dynamically (usually with php)
     * generated file */
    QList<QByteArray>   h = reply->rawHeaderList();
    foreach(QByteArray x,h) {
        qDebug() << x << "\t" << reply->rawHeader(x);
    QString                 baseFileName;
    if(!reply->rawHeader("Content-Disposition").isEmpty()) {
        baseFileName = QString(reply->rawHeader("Content-Disposition")).replace(QRegular[removed]"(.*filename=)\"(.*)\""),"\\2");
    } else {
        baseFileName = reply->url().path().split("/").last();
    fileName = QFileDialog::getSaveFileName(this, tr("Save File"), QDir::currentPath() + "/" + baseFileName);


    MainWindow::slot_downloadProgress(qint64 bytesReceived, qint64 bytesTotal)

    QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());

    switch(reply->error()) {
        case    QNetworkReply::NoError:
        qDebug() << "Download Finished!";
            QFile   file&#40;this->fileName&#41;;
            if(;) {
                QByteArray data = reply->readAll();
               qDebug() << "Writted " <<  file.write(data) << " BYTES";



Log in to reply