PDA

View Full Version : How to connect to ftp server and read the filenames in the ftp folder?



Gokulnathvc
9th October 2012, 07:02
How to connect to ftp server and read the filenames from the ftp folder and create labels with the filenames and display those on the dialog?

ChrisW67
9th October 2012, 07:09
What have you tried?

Gokulnathvc
9th October 2012, 07:15
I have just made the connection using ftp. How to read the filenames in the particular folder?

ChrisW67
9th October 2012, 09:10
Issue the FTP LIST command. So far you have not explained how you have tried to use Qt to do any of this.

Gokulnathvc
9th October 2012, 10:18
I have added the following code to login into the ftp site.
QFtp *ftp = new QFtp(this);
ftp->connectToHost("ftp.qt.nokia.com");
ftp->login();

wysota
9th October 2012, 12:02
And then? Have you seen QFtp::list()?

Gokulnathvc
9th October 2012, 14:31
I have found the way to get the list of filenames from the ftp folder.
#include "ftpwindow.h"
#include "ui_ftpwindow.h"
#include "qftp.h"
#include "qurl.h"
#include "QMessageBox"
#include "QFile"

FTPWindow::FTPWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::FTPWindow),ftp(0)
{
ui->setupUi(this);

QUrl url("ftp.qt.nokia.com");

}

FTPWindow::~FTPWindow()
{
delete ui;
}

void FTPWindow::changeEvent(QEvent *e)
{
QDialog::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}

void FTPWindow::addToList(const QUrlInfo &urlInfo)
{
QString filelist;
filelist += urlInfo.name();
//QMessageBox::information(this, tr("FTP"),filelist,QMessageBox::Ok);

QFile newfile("ftpfilenames1.txt");
newfile.open(QIODevice::WriteOnly | QIODevice::Append);
newfile.write(filelist.toAscii().data());
newfile.write("\r\n");
newfile.close();

}

void FTPWindow::on_Connect_clicked()
{
#ifdef Q_OS_SYMBIAN
if(!bDefaultIapSet) {
qt_SetDefaultIap();
bDefaultIapSet = true;
}
#endif
if (ftp) {
ftp->abort();
ftp->deleteLater();
ftp = 0;
//![0]

#ifndef QT_NO_CURSOR
setCursor(Qt::ArrowCursor);
#endif
return;
}

#ifndef QT_NO_CURSOR
setCursor(Qt::WaitCursor);
#endif

//![1]
ftp = new QFtp(this);
connect(ftp, SIGNAL(commandFinished(int,bool)),
this, SLOT(ftpCommandFinished(int,bool)));
connect(ftp, SIGNAL(listInfo(QUrlInfo)),
this, SLOT(addToList(QUrlInfo)));

QUrl url("ftp.qt.nokia.com");
if (!url.isValid() || url.scheme().toLower() != QLatin1String("ftp")) {
ftp->connectToHost("ftp.qt.nokia.com", 21);
ftp->login();
} else {
ftp->connectToHost(url.host(), url.port(21));

if (!url.userName().isEmpty())
ftp->login(QUrl::fromPercentEncoding(url.userName().toL atin1()), url.password());
else
ftp->login();
if (!url.path().isEmpty())
ftp->cd(url.path());
}


}

void FTPWindow::ftpCommandFinished(int, bool error)
{
#ifndef QT_NO_CURSOR
setCursor(Qt::ArrowCursor);
#endif

if (ftp->currentCommand() == QFtp::ConnectToHost) {
if (error) {
QMessageBox::information(this, tr("FTP"),
tr("Unable to connect to the FTP server "
"at %1. Please check that the host "
"name is correct."),
QMessageBox::Ok);
on_Connect_clicked();
return;
}
return;
}

if (ftp->currentCommand() == QFtp::Login){
ftp->list(); }

else if (ftp->currentCommand() == QFtp::List) {


}

}



This creates a file ftpfilenames1.txt and it contains all the filenames from the ftp site. Works fine.

Gokulnathvc
15th October 2012, 07:03
I am getting all the lists including file and folder names. I just want to list only the filenames. How to filter in order to get the filenames alone?

ChrisW67
15th October 2012, 08:13
Have you looked at the docs for QUrlInfo, the class you are using to get the directory entry name, or did you just copy'n'paste from somewhere else?

Gokulnathvc
15th October 2012, 08:28
Thanks.. I have used isDir() and it worked... voilaaaaa

mahi6a1985
15th December 2012, 10:04
And then? Have you seen QFtp::list()?

Hi Wysota,

i am having a small doubt. In the QT installed directory i found the example of "QFTP", which is used to connect to FTP Server.
Here my doubt is, can we connect to a remote machine by using an "host-name" of a remote system instead of "ftp.qt.nokia.com" in the same FTP example ?

wysota
15th December 2012, 10:05
"ftp.qt.nokia.com" is a host name.

mahi6a1985
15th December 2012, 10:09
yes i agree but i have given the ip address of the other machine in that ftp example i can able to connect it but cant see the files list.

My aim is to connect to a remote system (windows OS) from my Linux system and copy a directory of windows system (i.e., which consists of 2 or 3 text files) to my linux system.

wysota
15th December 2012, 10:21
yes i agree but i have given the ip address of the other machine in that ftp example i can able to connect it but cant see the files list.

Maybe it's a matter of being able to perform active/passive FTP connection.

mahi6a1985
15th December 2012, 10:35
Maybe it's a matter of being able to perform active/passive FTP connection.

yes you are absolutely correct my ftp server application which in windows wont accept passive FTP connection.
So, i have to make some changes then... ??

Thanks wysota

wysota
15th December 2012, 11:35
You have to act accordingly to the protocol approach you wish to support.

mahi6a1985
18th December 2012, 09:58
You have to act accordingly to the protocol approach you wish to support.

Hi wysota,

Now i am able to connect to ftp server and can view the entire remote system FTP server files and also i can download them but the problem is some of them were 0-bytes means only file is downloaded non of the content is downloaded and some of them were half downloaded.
But all the files listed in the FTP site were downloaded to my local directory.

what could be the problem ?

i have seen the QFTP example their its only one file at a time so it seems fine. But in my case i want to download all the files of the ftp folder.

wysota
18th December 2012, 10:15
And how are you doing that?

mahi6a1985
18th December 2012, 10:34
And how are you doing that?

After getting the list using ftp->list();
i am using a loop and calling each of the listed files one by one using ftp->get(urlInfo.name(),file);

wysota
18th December 2012, 10:53
Show the actual code, please.

mahi6a1985
18th December 2012, 11:08
Show the actual code, please.


localDir = "/home/nn";
QUrl url("ftp://192.168.1.146");
ftp->connectToHost(url.host(),url.port(21));
ftp->login();
connect(ftp, SIGNAL(commandFinished(int,bool)), this, SLOT(ftpCommandFinished(int,bool)));
connect(ftp, SIGNAL(listInfo(QUrlInfo)), this, SLOT(ftpListInfo(QUrlInfo)));



void MainWindow::ftpCommandFinished(int, bool error)
{
if(ftp->currentCommand() == QFtp::ConnectToHost)
{
ftp->login();
}

if(ftp->currentCommand() == QFtp::Login)
{
ftp->list();
}

if(ftp->currentCommand() == QFtp::Get)
{
file->close();
}

if(ftp->currentCommand() == QFtp::List)
{
download(listF);
}
}



void MainWindow::ftpListInfo(QUrlInfo urlInfo)
{
if(urlInfo.isFile() && urlInfo.isReadable())
{
listF.append(urlInfo);
}
}



void MainWindow::download(QList<QUrlInfo> listF)
{
foreach(QUrlInfo urlInfo, listF)
if(urlInfo.isFile() && urlInfo.isReadable())
{
file = new QFile(localDir + "/" + urlInfo.name());
file->open(QIODevice::WriteOnly);
ftp->get(urlInfo.name(), file);
}
}

wysota
18th December 2012, 11:17
You're not checking that each file actually gets downloaded. When you get commandFinished() after "get", you close "file" which is a global variable. Since QFtp::get() only schedules a download, your foreach loop completes before the first download is even started. As a result you end up with "file" assigned with the file belonging to the last "get" request but when the first "get" request completes, you close the last file (and not the first one, so the first one remains open and the last one is closed with 0 size). You need to pay attention to what QFtp::get() returns and associate it with the file handler so that you can retrieve the handle back when you receive commandFinished() signal. And fix those memory leaks.

mahi6a1985
18th December 2012, 11:20
You're not checking that each file actually gets downloaded. When you get commandFinished() after "get", you close "file" which is a global variable. Since QFtp::get() only schedules a download, your foreach loop completes before the first download is even started. As a result you end up with "file" assigned with the file belonging to the last "get" request but when the first "get" request completes, you close the last file (and not the first one). You need to pay attention to what QFtp::get() returns and associate it with the file handler so that you can retrieve the handle back when you receive commandFinished() signal. And fix those memory leaks.

Ok, i will try that. thanks

wysota
18th December 2012, 11:25
Your code is wrong. Period.

mahi6a1985
18th December 2012, 12:13
Your code is wrong. Period.

I got it... the problem solved.

mahi6a1985
24th December 2012, 08:19
Hi wysota,

My ftp application is working fine in my desktop machines, whereas i am getting the problem in My touchscreen device. Which consists of Debian Linux.

when i run the ftp application in the device its showing an error message as follows :


./qftp: symbol lookup error: ./qftp: undefined symbol: _ZN9QListData11detach_growEPii

why it is happening i am not able to find ?

Added after 59 minutes:

I got it problem solved.

mahi6a1985
26th December 2012, 12:23
Hi wysota,

While using ftp in my device it saying "Read-Only file system".
i even tried it by giving the folder and files read write permissions, then also i am unable to copy the files from my PC to the Device.

wysota
26th December 2012, 19:59
And what exactly do you want me to do about it? :) Your filesystem is probably mounted read-only.