Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
While using setHost statement, It retuns error "undefined reference".
How to download a file using HTTP with URL?


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;
http=new QHttp(this);
setHost(url.host(),QHttp::ConnectionModeHttp);
Posted
Updated 7-Mar-11 4:14am
v2
Comments
HimanshuJoshi 7-Mar-11 10:15am    
Edited to add <pre> tags around your code

The QHttp class used in your code is obsolete[^]. New code should use the QNetworkAccessManager[^] class instead. See the example code[^] in the Qt documentation.
 
Share this answer
 
Simplest way to download from the HTTP is below::
connect(&http,SIGNAL(done(bool)),this,SLOT(httpdon e())); //added in the constructor..


In the header file......Make the following changes...

QHttp http;
QFile file;


private slots:
bool on_pushButton_clicked();
void httpdone();


signals:
void done();


in the Qthttp.cpp file...

bool QtHttp:n_pushButton_clicked()
{

QString strUrl="http://www.blabla.com//file";
QUrl url = QUrl::fromUserInput(strUrl);

QFileInfo fileInfo(url.path());
QString strhost=url.encodedHost();
QString filename=fileInfo.fileName();
file.setFileName("C:\\Qt\\QHttp\\"+filename);
http.setHost(url.host(),url.port(80));
http.get(url.path(),&file);

if(!file.open(QIODevice::WriteOnly))
{
QMessageBox::warning(NULL,"warning","file is not opened",QMessageBox::Ok);
}
file.write(http.readAll());
http.close();
return true;

}

void QtHttp::httpdone()
{

file.close();
Q_EMIT done();
}
 
Share this answer
 
v2
Comments
[no name] 22-Mar-11 4:46am    
Edited for code tags.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900