如何在QT中使用带有URL的HTTP下载文件?与下面的代码. [英] How to download a file using HTTP with URL in QT? with the code below.

查看:244
本文介绍了如何在QT中使用带有URL的HTTP下载文件?与下面的代码.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用以下代码通过http下载文件:无错误.

但是该文件未下载::

I have used the code below to download a file using http: No errors.

But the file is not get downloaded::

  QUrl url = QUrl::fromUserInput(strUrl);

      QFileInfo fileInfo(url.path());
 
      QString fileName=fileInfo.fileName();
  
      if (QFile::exists(fileName)) {
  
      QMessageBox::information(this, tr("HTTP"),
  
      tr("There already exists a file called %1 in "
  
      "the current directory.")
 .arg(fileName));
   
      }
 
       

      file = new QFile(fileName);

      if (!file->open(QIODevice::WriteOnly)) {

      QMessageBox::information(this, tr("HTTP"),

      tr("Unable to save the file %1: %2.")
 
      .arg(fileName).arg(file->errorString()));

      delete file;

      file = 0;
      }

       

      file.setFileName(fileName);
     file.open(QIODevice::WriteOnly);

      QHttp http;
    QEventLoop loop;
    QFile file;
    QObject::connect(&http, SIGNAL(done(bool)), &loop, SLOT(quit()));

    file.setFileName(filename);
    file.open(QIODevice::WriteOnly);

    http.setHost(url.host(), url.port(80));
    http.get(url.toEncoded(QUrl::RemoveScheme | QUrl::RemoveAuthority), 
&file);

    loop.exec();

推荐答案

您不检查API connect get 上的错误返回.检查错误代码是什么,然后在获得该信息后再次在此处发布.
You''re not checking the error returns on the APIs connect and get (among others). Check what the error code is, and then post here again when you have that information.


这篇关于如何在QT中使用带有URL的HTTP下载文件?与下面的代码.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆