PDA

View Full Version : how to use qftp to upload file in just one procedure?



cxl2253
23rd April 2007, 08:16
I just want to use qftp to upload a file .at first , i have to determine whether the dir exists or not. But how to implement the code to judge and at the same time upload file in one function?
just like that :
Qstringlist dirlist;
ftp.list(dirlist) ;
if (!dirlist.contain(onedir))
{
ftp.mkdir(onedir);
}
ftp.put(myFile) ;

marcel
23rd April 2007, 08:17
Not sure what you mean. What is wrong with the code you posted?

cxl2253
23rd April 2007, 10:12
before upload a file ,i want to determine if the parent dir existed . but how to do it? i know the flowing code will not work.



bool frmMainWindow::upLoadFile(QString checkcode,QString filename)
{
ftp = new QFtp(this);
connect(ftp, SIGNAL(commandFinished(int, bool)),
this, SLOT(ftpCommandFinished(int, bool)) );
connect(ftp, SIGNAL(listInfo(const QUrlInfo &)),
this, SLOT(ftpListInfo(const QUrlInfo &)));
connect(ftp, SIGNAL(dataTransferProgress(qint64, qint64) ),
this, SLOT(updateDataTransferProgress(qint64, qint64)));

ftp->connectToHost("192.168.1.3");
ftp->login("ftpuser","ftpuser");
fileList.clear();
ftp->cd("/pisdata/"); //init dir
ftp->list();

QString taskcode;
taskcode=checkcode.left(8);

//first to determine if parent dir exits.
if (!fileList.contains(taskcode)){ //fileList will not contain anything
ftp->mkdir(taskcode);
ftp->cd(taskcode);
ftp->mkdir("pdf");
ftp->mkdir("image");
ftp->mkdir("xml");
ftp->cd("image");
}
else{
ftp->cd(taskcode);
fileList.clear();
ftp->list();
if(!fileList.contains("image",Qt::CaseInsensitive)){
ftp->mkdir("image");
}
ftp->cd("image");
}
//////////////start upload file////////
QString ftpPath;
ftpPath="/pisdata/"+taskcode;
ftpPath+="/image/";
ftp->cd(ftpPath);
//QFile file(filename);
file=new QFile(filename);
if (!file->open(QIODevice::ReadOnly))
{
file->close();
delete file;
return false;
}
QFileInfo fi(filename);
ftp->put(file,fi.fileName());
progressDialog->setLabelText(tr("put %1...").arg(fi.fileName()));
progressDialog->exec();

return true;
}

void frmMainWindow::ftpListInfo(const QUrlInfo &urlInfo)
{
fileList<<urlInfo.name() ;
}

marcel
23rd April 2007, 10:28
The problem is that you do not wait for the ftp commands to complete ( either success or fail ). You do everything at once.

You should wait for every ftp command to complete and then go to next step.
You should make use of the commandStarted and commandFinished signals and queue the command id's as soon as you execute them( for list, mkdir, etc ).

Currently when you search in the file list it will be empty, because the list() command has not completed yet.

Regards

cxl2253
23rd April 2007, 10:57
yes, i know the reason.But how should i do ? write the code in ftpCommandFinished ?