简体   繁体   中英

Saving downloaded file in Qt

I have my slot being called whenever someone clicks the link and I know the file is there because I can retrieve the file name and the amount of bytes the file is just not sure how to s ave it after I call the QFileDialog::getSaveFileName? I know that gives me the name of the file if the user decides to change it but how do I get the location they decide to save it in and then write it to that location.

NB: The file they will download is a word doc if that makes any difference?

void MainWindow::unsupportedContent(QNetworkReply *reply) {

    qDebug() << "Left click - download!";
    qDebug() << "Bytes to download: " << reply->bytesAvailable();

    QString str = reply->rawHeader("Content-Disposition");

    QString end = str.mid(21);
    end.chop(1);

    qDebug() << "string: " << end;

    qDebug() << "File name: " << reply->rawHeader("Content-Disposition");
    qDebug() << "File type: " << reply->rawHeader("Content-Type");
    QString defaultFileName = QFileInfo(end).fileName();
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), defaultFileName);
    if (fileName.isEmpty()) return;


    QFile *file = new QFile(fileName);
    file->open(fileName);
    file->write(reply->read(reply->bytesAvailable()));
    file->close();
}

today i explained it on another post so i will link that post here : Post i can tell you that you have only to implement a slot that write into the file you created. and call it when readyRead() signal is emitted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM