PDA

View Full Version : QFtp : Uploading a directory



smshamim
30th August 2006, 13:37
Hi all,

Here is my problem:

Purpose:- Upload a directory from local host to remote machine (remote ftp server)

Qt Class in use:- QFtp Class

Problem:-

Source code given below, works fine if I put QmessageBox after line:
ftp->mkdir(directoryName);
Otherwise I get the error-

Changing directory failed:
Failed to change directory.


Source Code:-


uploadDirectory(QDir dir){

QString absPath=dir.absPath();

QString directoryName=absPath.section('/',-1);
dir.setSorting(QDir::DirsFirst);
const QFileInfoList *localFileInfoList = dir.entryInfoList();

QFileInfoListIterator fileIterator(*localFileInfoList );
int count=localFileInfoList->count();

QFileInfo* file;
int mkdirFlag =1;

for(int temp=0;temp<count;temp++) {

file=fileIterator.current();
if(mkdirFlag){

ftp->rawCommand( "PWD" );
ftp->mkdir(directoryName);
mkdirFlag=0;
currentDir->cd(directoryName);
QString ftpString=ftpDir+QString("/")+directoryName;
ftp->cd(ftpString);

}

if(file->isDir() && file->fileName()!="." && file->fileName()!=".."){
uploadDirectory(QDir(currentDir->absPath()+QString("/")+QString(file->fileName())));

}
if(!file->isDir())
uploadFile(currentDir->absPath()+QString("/")+file->fileName());

++fileIterator;
}
ftp->cd("..");

currentDir->cdUp();
}


Description:-

ftp:-denotes the remote ftp(Class QFtp)
currentDir:-denotes the current local directory(QDir)
ftpDir:-denotes current remote file path (QString)

Note: - we get this value by sending PWD command( ftp->rawCommand( "PWD") ) in the command reply function we set this value in ftpDir
directoryName:- if dir is “home/username/folder1” then directoryName gives folder1
uploadFile() function uploads the given file


Observation:-

It takes time to complete the function like get, put, mkdir, cd etc. and these functions are nonblocking. Program doesn’t block here. Just take an example if I call ‘mkdir’ (ftp->mkdir()) then program doesn’t wait for completion of this function. It goes to next line and performs (ftp->cd()) and I get the error.

How should I resolve this problem?

Thanks :)

munna
30th August 2006, 13:46
I think you should download the book c++ GUI Programming with Qt3 (http://dot.kde.org/1098950311/).

There a chapter called "Networking" which talks brilliantly about what you require.

patrik08
31st August 2006, 13:06
Hi all,

Here is my problem:

Purpose:- Upload a directory from local host to remote machine (remote ftp server)
How should I resolve this problem?

Thanks :)


Here is a snip from QT4 + libcurl easy on all OS....
Is possibel to mkdir realy "mkd" if remote dir not exist!





/* header #include <curl/curl.h> */
bool Qjobs::UploadFileServer( QString ftpfile , QString remotefile )
{
/* header #include <curl/curl.h> */
/* ftp upload to server file binary */
curl_global_init( CURL_GLOBAL_ALL ) ;
CURL *curl_handle = curl_easy_init() ;

if( NULL == curl_handle ){
return false;
}
QFileInfo QuoInfo(ftpfile);
long fzize = QuoInfo.size(); /* file size */
/* ftpfile = c:\image.jpg / remotefile = ftp://username:pass@host.com:21/path/image.jpg /path/ related to ftp login */
/* url = ftp:{login}:{password}@{host}:21{dated dir}{data remotefile} */
QByteArray lop = ftpfile.toAscii();
QByteArray ba = remotefile.toAscii();
char *localfile = lop.data();
FILE *putfile;
putfile = fopen(localfile, "rb"); /* binary image! */
char *url = ba.data();
/*
qDebug() << "### localfile " << localfile;
qDebug() << "### Puturl " << url;
*/
/* handy for debugging: see *everything* that goes on */
/* curl_easy_setopt( curl_handle , CURLOPT_VERBOSE, 1 ) ; */ /* open to debug to sea actions */
// target url:
curl_easy_setopt( curl_handle , CURLOPT_URL, url ) ;
// no progress bar:
curl_easy_setopt(curl_handle,CURLOPT_NOPROGRESS , 1 ) ;
// use passive FTP, if it's available
curl_easy_setopt(curl_handle,CURLOPT_FTP_USE_EPSV , 1 ) ;
curl_easy_setopt(curl_handle,CURLOPT_TIMEOUT , 15 ); /* put on server config qwidget */
curl_easy_setopt(curl_handle,CURLOPT_UPLOAD , 1 );
curl_easy_setopt(curl_handle,CURLOPT_INFILE , putfile );
curl_easy_setopt(curl_handle,CURLOPT_INFILESIZE , fzize );
/* make action */
if (curl_easy_perform(curl_handle)==CURLE_OK) {
curl_easy_cleanup( curl_handle );
curl_global_cleanup();
return true;
} else {
return false;
}
}





// yes, it's "mkd" and not "mkdir" (many clients translate it for you)
// see RFC 959 for details:
//
// http://www.ietf.org/rfc/rfc959.txt
//
// NOTE: creating/changing to the dir isn't necessary here; it's done as
// part of the example of using CURLOPT_QUOTE. You could simply have
// specified CURLOPT_FTP_CREATE_MISSING_DIRS.

commandBuf << "mkd " << remotePath << std::flush ;
const std::string mkdirCommand = commandBuf.str() ;
commands = curl_slist_append( commands , mkdirCommand.c_str() ) ;

// NOTE: cURL will try to change dirs (based on the URL)
// from the present directory (by default, your home dir
// on the remote system).

// To upload data to an absolute path on the remote host,
// you must change to the root dir first.
commands = curl_slist_append( commands , "cwd /" ) ;
// from here, cURL will "cwd tmp" and "cwd {timestamp dir}"