PDA

View Full Version : how to download images using URL link



iswaryasenthilkumar
9th March 2015, 05:43
i have link which contains image..i have to download this image using Qt.i have tried bt i dont get to download..:confused:
can any one give me suggestion for this:confused:
Thanks in Advance:o
below code which i tried so far:



QUrl url1("http://103.3.229.181/digital-photo-frame-ads\/public/uploads/07032015/qzms7p5nfcza55539.jpg");
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(netwManagerFinished(QNetworkReply*)));
QNetworkRequest request(url1);
manager->get(request);


void Widget::netwManagerFinished(QNetworkReply *reply)
{
QByteArray jpegData = pic.toLocal8Bit();
QPixmap pixmap;
pixmap.loadFromData(jpegData);
QPixmap scaled = pixmap.scaled(128,64,Qt::IgnoreAspectRatio, Qt::FastTransformation);
l.setPixmap(scaled);
l.show();
QString filename = QFileDialog::getSaveFileName(this, tr("Save Skin"), "/home/ihorse1/10-03-2015", ,tr("JPEG Image(*.jpg)"));
QFile file (filename);
file.open(QIODevice::WriteOnly);
QDataStream out (&file);
out << reply;
qDebug()<<out;
file.close();
}

jefftee
9th March 2015, 06:00
Where do you read the reply data?

I would expect to see a reply->readAll() in your netwManagerFinished slot that reads the data retrieved by the get request. You should also check the http status code when the request is finished.

Lastly, please use the
tags when posting your code, not
tags.

iswaryasenthilkumar
9th March 2015, 06:42
i made changes sir,,
bt i got an arror as
QPixmap::scaled: Pixmap is a null pixmap
QFSFileEngine::open: No file name specified
QIODevice::putChar: Closed device
QVariant(, )
QPixmap::scaled: Pixmap is a null pixmap
QFSFileEngine::open: No file name specified
QIODevice::putChar: Closed device
QVariant(, )
my code below:


QUrl url("http://103.3.229.181/digital-photo-frame-ads/public/getImages");//this link contains two images
QNetworkAccessManager manager;
QNetworkReply *response = manager.get(QNetworkRequest(QUrl(url)));
QEventLoop event;
connect(response,SIGNAL(finished()),&event,SLOT(quit()));
event.exec();
QString html = response->readAll();
if(html.length() > 0){
QStringList str;
str = html.split(",");
for (int i = 0; i < str.size(); ++i)
{
pic=pic.remove(QRegExp(QString::fromUtf8("[`~!@#$%^&*()_—+=|;<>«»,?{}\'\"\\\[\\\]\\\\]")));
QByteArray jpegData = pic.toLocal8Bit();
QPixmap pixmap;
pixmap.loadFromData(jpegData);
QPixmap scaled = pixmap.scaled(128,64,Qt::IgnoreAspectRatio, Qt::FastTransformation);
l.setPixmap(scaled);
l.show();
QString filename = QFileDialog::getSaveFileName(this, tr("Save Skin"), "/home/ihorse1/10-03-2015",tr("JPEG Image(*.jpg)"));
QFile file (filename);
file.open(QIODevice::WriteOnly);
QDataStream out (&file);
out << reply;
qDebug()<<out;
file.close();
}
}


Where do you read the reply data?

I would expect to see a reply->readAll() in your netwManagerFinished slot that reads the data retrieved by the get request. You should also check the http status code when the request is finished.

Lastly, please use the
tags when posting your code, not tags.

jefftee
9th March 2015, 07:10
Line 14 makes no sense at all. Are your loading a URL into the QByteArray expecting it to contain the actual image data? Everything after that will fail because your QByteArray doesn't contain valid image data, so obviously line 16 will fail also.

I suggest that you start over and try to successfully get a single URL that contains image data and go from there. Use QNetworkAccessManager->get() to issue the http request for the actual image, then in your slot connected to finished(), reply->readAll() the data into a QByteArray. Once you have done that successfully, then you can try to create your QPixmap and write image data to a file, etc.

What you just posted now is different than your first post (and you also have a similar post earlier in this same forum). You make it very hard for someone to help you because of your shotgun approach to posting wildly different non-compilable and non-functional code each time you post.

Good luck to you.

BTW, wysota already stated in your other thread on this topic that your data is apparently in JSON format. If you process this using the QJson* classes, you won't have to jump through all of the hoops you are currently attempting with the returned URLs.

iswaryasenthilkumar
9th March 2015, 07:28
how to implement JSON format ..give some example

wysota
9th March 2015, 07:43
http://lmgtfy.com?q=Qt+JSON

jefftee
9th March 2015, 07:46
The Qt JSON documentation contains examples, so please read the documentation for QJsonDocument, QJsonObject, QJsonArray, etc.

Additional documentation is available here (http://doc.qt.io/qt-5/json.html) and here (http://doc.qt.io/qt-5/qtcore-json-savegame-example.html) and of course there is always google.

iswaryasenthilkumar
9th March 2015, 13:07
i have one doubt which method is used to download images by url in qt

anda_skoa
9th March 2015, 13:27
QNetworkAccessManager::get()

Cheers,
_

iswaryasenthilkumar
10th March 2015, 11:43
i have one more doubt
http://103.3.229.181/digital-photo-frame-ads/public/uploads/11032015/wqpc0ffvz23l90653.jpg
based on the above link i need to get only wqpc0ffvz23l90653.jpg what method is used to get this input,,pls give me suggestion for this
Thanks in advance

wysota
10th March 2015, 11:58
QUrl::fileName()