PDA

View Full Version : QFtp upload file to server



djo87
26th May 2012, 11:19
Hi,
I use QFtp class in order to upload my files to server but after ftp->put I find that files are empty in server
I do not understand what is the problem in the code
My code is here :)

QFtp *ftp = new QFtp(this);
ftp->setTransferMode(QFtp::Passive);
//connect(ftp,SIGNAL(commandFinished ( int, bool)),this,SLOT(fincommande()));
ftp->connectToHost("ftp.icwus.net",21);
//setValueProgressBar(25);

connect(ftp, SIGNAL(stateChanged(int)), this, SLOT(showNewState(int)));

connect( ftp, SIGNAL( commandFinished( int, bool )),this, SLOT( ftp_commandFinished( int, bool )) );
connect( ftp, SIGNAL( commandFinished( int, bool )),this, SLOT( ftp1_commandFinished( int, bool )) );

ftp->login("mylogin","mypassword");
ftp->cd("acrgafsa");
ftp->mkdir("timetables");
ftp->cd("timetables");

int i = 0;
QString filename;
QString files = QFileDialog::getOpenFileName(this, tr("Select File"),
"D://");

if (files.isEmpty())
return;

while(files.indexOf("/", i) != (-1)) //find the file name not include the full url
i = files.indexOf("/",i) + 1;
while(i < files.size())
{
filename.append(files.at(i));

i++;
}

QFile *file = new QFile(filename);

Q_ASSERT(file != NULL);

if(!file->open(QIODevice::ReadWrite)) //finished create the file in the debug file
{
qDebug() << "opern err" << file->fileName();
return;
}

ftp->put(file,filename);

Ginsengelf
29th May 2012, 08:52
Hi, do you reach the ftp_commandFinished() slot? Does its bool parameter indicate success?

Ginsengelf

ChrisW67
30th May 2012, 02:55
This is what is see when I read through your program:

Ask the user for a source file name and get a full path [19]
Use a non-portable method to find the base name of the source file [25-32] and throw away the full path. Try QFileInfo.
Allocate QFile object on the heap and use the base name of the source file [34]. Do you ever free this memory?
Check for a null return from the new call [36]. By default C++ will throw std::bad_alloc if new fails and this line will never be reached.
Open that file using ReadWrite mode, in spite of the obvious need only to read [38]. The file with the specified name does not exist in the current working directory but will be created because you said you want to write to it. The open does not fail.
You put() the empty file [44]
The server gets an empty file

Seems like everything is working as coded.