Partage
  • Partager sur Facebook
  • Partager sur Twitter

[Qt] Erreur lors du téléchargement d'un fichier

    19 mars 2008 à 14:21:16

    Bonjour,

    Je suis en train de créer un logiciel basé sur le framework Qt.
    J'ai besoin de télécharger un fichier grâce à QHttp depuis un site web qui doit générer un fichier xml, mais, lors de l'ouverture du logiciel, le fichier est crée dans le répertoire principal (release), mais, il n'y a rien dedans.

    Code de mainform.cpp :
    1. #include <QtGui>
    2. #include <QtNetwork>
    3. #include <QRegExp>
    4. #include "mainform.h"
    5. MaFenetre::MaFenetre() : QWidget()
    6. {
    7. http = new QHttp(this);
    8.         connect(http, SIGNAL(requestFinished(int, bool)),
    9.         this, SLOT(httpRequestFinished(int, bool)));
    10.         connect(http, SIGNAL(dataReadProgress(int, int)),
    11.         this, SLOT(updateDataReadProgress(int, int)));
    12.         connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
    13.         this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
    14.         connect(http, SIGNAL(authenticationRequired(const QString &, quint16, QAuthenticator *)),
    15.         this, SLOT(slotAuthenticationRequired(const QString &, quint16, QAuthenticator *)));
    16.         downloadFile();
    17. }
    18. void MaFenetre::downloadFile()
    19. {
    20.         QUrl url("http://host/file.php");
    21.         QFileInfo fileInfo(url.path());
    22.         QString fileName = fileInfo.fileName();
    23.         if (fileName.isEmpty())
    24.         fileName = "index.html";
    25.         if (QFile::exists(fileName)) {
    26.                 if (QMessageBox::question(this, tr("HTTP"),
    27.                                         tr("There already exists a file called %1 in "
    28.                                                 "the current directory. Overwrite?").arg(fileName),
    29.                                         QMessageBox::Ok|QMessageBox::Cancel, QMessageBox::Cancel)
    30.                                 == QMessageBox::Cancel)
    31.                 return;
    32.                 QFile::remove(fileName);
    33.         }
    34.         file = new QFile(fileName);
    35.         if (!file->open(QIODevice::WriteOnly)) {
    36.                 QMessageBox::information(this, tr("HTTP"),
    37.                 tr("Unable to save the file %1: %2.")
    38.                 .arg(fileName).arg(file->errorString()));
    39.                 delete file;
    40.                 file = 0;
    41.                 return;
    42.         }
    43.         http->setHost(url.host(), 80);
    44.         http->setUser("user", "password");
    45.         httpRequestAborted = false;
    46.         httpGetId = http->get(url.path(), file);
    47. }
    48. void MaFenetre::httpRequestFinished(int requestId, bool error)
    49. {
    50.         if (requestId != httpGetId)
    51.         return;
    52.         if (httpRequestAborted) {
    53.                 if (file) {
    54.                         file->close();
    55.                         file->remove();
    56.                         delete file;
    57.                         file = 0;
    58.                 }
    59.                 return;
    60.         }
    61.         if (requestId != httpGetId)
    62.         return;
    63.         file->close();
    64.         if (error) {
    65.                 file->remove();
    66.                 QMessageBox::information(this, tr("HTTP"),
    67.                 tr("Download failed: %1.")
    68.                 .arg(http->errorString()));
    69.         } else {
    70.                 QString fileName = "http://host/file.php";
    71.         }
    72.         delete file;
    73.         file = 0;
    74. }
    75. void MaFenetre::readResponseHeader(const QHttpResponseHeader &responseHeader)
    76. {
    77.         if (responseHeader.statusCode() != 200) {
    78.                 QMessageBox::information(this, tr("HTTP"),
    79.                 tr("Download failed: %1.")
    80.                 .arg(responseHeader.reasonPhrase()));
    81.                 httpRequestAborted = true;
    82.                 http->abort();
    83.                 return;
    84.         }
    85. }
    86. void MaFenetre::updateDataReadProgress(int bytesRead, int totalBytes)
    87. {
    88.         if (httpRequestAborted)
    89.         return;
    90. }


    Code de mainform.h :
    1. #ifndef MAINFORM_H
    2. #define MAINFORM_H
    3. #include <QHttp>
    4. class MaFenetre: public QWidget
    5. {
    6. Q_OBJECT;
    7. public:
    8. //TELECHARGEMENT
    9.         QHttp *http;
    10.         QFile *file;
    11.         int httpGetId;
    12.         bool httpRequestAborted;
    13.         int iDOWState;
    14. MaFenetre();
    15. public slots:
    16.         //TELECHARGEMENT
    17.         void downloadFile();
    18.         void httpRequestFinished(int requestId, bool error);
    19.         void readResponseHeader(const QHttpResponseHeader &responseHeader);
    20.         void updateDataReadProgress(int bytesRead, int totalBytes);
    21. };
    22. #endif


    Aidez moi s'il vous plaît !
    • Partager sur Facebook
    • Partager sur Twitter

    [Qt] Erreur lors du téléchargement d'un fichier

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown