How To Read a Text File Contents Located on a FTP Server
-
Hi
Since you are using the QFtp class then the old samples can be useful.
This list and downloads files from ftp
http://doc.qt.io/archives/qt-4.8/qt-network-qftp-example.htmlOverall the process is.
// connect finished signal ftp = new QFtp(this); connect(ftp, SIGNAL(commandFinished(int,bool)), this, SLOT(ftpCommandFinished(int,bool))); // login etc // ask for file ftp->get(filename, file);
The void FtpWindow::downloadFile() should be what you need.
-
I have written a program to login to my ftp account:
Here
Now i want to read a text file Located on my FTP server and search for some special strings in it; if there is do something if not do another.
how can I do this? -
@rezaMSLM said in How To Read a Text File Contents Located on a FTP Server:
when using QFtp::get() method to download a file from server where is the file stored?
You provide a pointer to a
QFile
as second parameter ofget()
. So it's up to you where the file lands.Just have a deep look the the Example that was recommended you by @mrjj already.
-
@rezaMSLM said in How To Read a Text File Contents Located on a FTP Server:
when using QFtp::get() method to download a file from server where is the file stored?
You provide a pointer to a
QFile
as second parameter ofget()
. So it's up to you where the file lands.Just have a deep look the the Example that was recommended you by @mrjj already.
@aha_1980 said in How To Read a Text File Contents Located on a FTP Server:
Just have a deep look the the Example that was recommended you by @mrjj already.
I tried reading that example and some other codes on the internet But I'm a beginer and the examples were Confusing for me !
for first step I just want to download a text file located on FTP server into my desktop.
would any one please write a sample code for me doing that? thinking on simple sample codes will help me much. -
@aha_1980 said in How To Read a Text File Contents Located on a FTP Server:
Just have a deep look the the Example that was recommended you by @mrjj already.
I tried reading that example and some other codes on the internet But I'm a beginer and the examples were Confusing for me !
for first step I just want to download a text file located on FTP server into my desktop.
would any one please write a sample code for me doing that? thinking on simple sample codes will help me much. -
@rezaMSLM It is up to you where to store the file. You can use http://doc.qt.io/qt-5/qstandardpaths.html#locate to get a directory (for example the temp directory).
-
@rezaMSLM It is up to you where to store the file. You can use http://doc.qt.io/qt-5/qstandardpaths.html#locate to get a directory (for example the temp directory).
-
Where i am wrong?
MainWindow::MainWindow(...) { my_file=new QFile("Qt_Test.txt");//create new file named Qt_Test.txt QTemporaryFile(QDir::tempPath());//store that file in temp path . . } void MainWindow::on_pushButton_Download_clicked() { ftp->get("Qt_Test.txt",my_file,QFtp::Ascii);//download Qt_Test.txt which is located on server }
I Receive no error but no file is downloaded to temp folder
-
Where i am wrong?
MainWindow::MainWindow(...) { my_file=new QFile("Qt_Test.txt");//create new file named Qt_Test.txt QTemporaryFile(QDir::tempPath());//store that file in temp path . . } void MainWindow::on_pushButton_Download_clicked() { ftp->get("Qt_Test.txt",my_file,QFtp::Ascii);//download Qt_Test.txt which is located on server }
I Receive no error but no file is downloaded to temp folder
@rezaMSLM Did you check the example? http://doc.qt.io/archives/qt-4.8/qt-network-qftp-example.html
MainWindow::MainWindow(...) { } void MainWindow::on_pushButton_Download_clicked() { QString path = QDir::tempPath() + '/' + "Qt_Test.txt"; my_file=new QFile(path); my_file->open(QIODevice::WriteOnly); ftp->get("Qt_Test.txt",my_file,QFtp::Ascii);//download Qt_Test.txt which is located on server }
-
@rezaMSLM Did you check the example? http://doc.qt.io/archives/qt-4.8/qt-network-qftp-example.html
MainWindow::MainWindow(...) { } void MainWindow::on_pushButton_Download_clicked() { QString path = QDir::tempPath() + '/' + "Qt_Test.txt"; my_file=new QFile(path); my_file->open(QIODevice::WriteOnly); ftp->get("Qt_Test.txt",my_file,QFtp::Ascii);//download Qt_Test.txt which is located on server }
@jsulm said in How To Read a Text File Contents Located on a FTP Server:
Did you check the example? http://doc.qt.io/archives/qt-4.8/qt-network-qftp-example.html
yes
the file is created but is empty while the program is open!
-
@jsulm said in How To Read a Text File Contents Located on a FTP Server:
Did you check the example? http://doc.qt.io/archives/qt-4.8/qt-network-qftp-example.html
yes
the file is created but is empty while the program is open!
@rezaMSLM
I could be wrong, but in @jsulm's code immediately after theftp->get()
line I would add line:my_file->close()
I see that the example in the docs does not do that. Which seems a bit surprising, but the docs may know better than I do. However, assuming the file close is needed, you would get exactly the behaviour you describe (empty till exit program) if you do not close it. So I would give it a try and see....
EDIT My assumption here was wrong, see further post below.
-
@rezaMSLM
I could be wrong, but in @jsulm's code immediately after theftp->get()
line I would add line:my_file->close()
I see that the example in the docs does not do that. Which seems a bit surprising, but the docs may know better than I do. However, assuming the file close is needed, you would get exactly the behaviour you describe (empty till exit program) if you do not close it. So I would give it a try and see....
EDIT My assumption here was wrong, see further post below.
-
@rezaMSLM
Hang on, I've just read more in the docs, as follows:On the other hand, if you only want to work with the complete data, you can connect to the commandFinished() signal and read the data when the get() command is finished.
The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by commandStarted() and commandFinished().
When the command is started the commandStarted() signal is emitted. When it is finished the commandFinished() signal is emitted.
So if I understand right, the
get()
merely initiates the transfer. I believe you need to write acommandFinished
slot, and close the file there.Ah yes, the example page goes on to show that. The example code has:
if (ftp->currentCommand() == QFtp::Get) { if (error) { statusLabel->setText(tr("Canceled download of %1.") .arg(file->fileName())); file->close(); file->remove(); } else { statusLabel->setText(tr("Downloaded %1 to current directory.") .arg(file->fileName())); file->close(); }
When a Get command is finished, a file has finished downloading (or an error occurred during the download).
I do believe the reason your file is empty is that it must be closed, at the correct time, before you will see contents.
I think you have to follow the whole example code, or perhaps download the source files from there and p,lay with the whole project if you want to see how to use it fully.
-
@hskoglund
Yes, but isn't username/password checking performed/errored much earlier than when he gets to issuing theget()
? It's performed during FTP connection open.The behaviour he describes --- file content not "visible" till exit application, then correct --- is explained exactly by failure to close output file, at least under Windows, which has a habit of marking files to be 0 length till closed by writer.