Results 1 to 3 of 3

Thread: QFtp : Uploading a directory

  1. #1
    Join Date
    Aug 2006
    Posts
    1
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Thumbs up QFtp : Uploading a directory

    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:-
    Qt Code:
    1. uploadDirectory(QDir dir){
    2.  
    3. QString absPath=dir.absPath();
    4.  
    5. QString directoryName=absPath.section('/',-1);
    6. dir.setSorting(QDir::DirsFirst);
    7. const QFileInfoList *localFileInfoList = dir.entryInfoList();
    8.  
    9. QFileInfoListIterator fileIterator(*localFileInfoList );
    10. int count=localFileInfoList->count();
    11.  
    12. QFileInfo* file;
    13. int mkdirFlag =1;
    14.  
    15. for(int temp=0;temp<count;temp++) {
    16.  
    17. file=fileIterator.current();
    18. if(mkdirFlag){
    19.  
    20. ftp->rawCommand( "PWD" );
    21. ftp->mkdir(directoryName);
    22. mkdirFlag=0;
    23. currentDir->cd(directoryName);
    24. QString ftpString=ftpDir+QString("/")+directoryName;
    25. ftp->cd(ftpString);
    26.  
    27. }
    28.  
    29. if(file->isDir() && file->fileName()!="." && file->fileName()!=".."){
    30. uploadDirectory(QDir(currentDir->absPath()+QString("/")+QString(file->fileName())));
    31.  
    32. }
    33. if(!file->isDir())
    34. uploadFile(currentDir->absPath()+QString("/")+file->fileName());
    35.  
    36. ++fileIterator;
    37. }
    38. ftp->cd("..");
    39.  
    40. currentDir->cdUp();
    41. }
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by smshamim; 30th August 2006 at 14:16.

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFtp : Uploading a directory

    I think you should download the book c++ GUI Programming with Qt3.

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

  3. #3
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QFtp : Uploading a directory

    Quote Originally Posted by smshamim
    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!



    Qt Code:
    1. /* header #include <curl/curl.h> */
    2. bool Qjobs::UploadFileServer( QString ftpfile , QString remotefile )
    3. {
    4. /* header #include <curl/curl.h> */
    5. /* ftp upload to server file binary */
    6. curl_global_init( CURL_GLOBAL_ALL ) ;
    7. CURL *curl_handle = curl_easy_init() ;
    8.  
    9. if( NULL == curl_handle ){
    10. return false;
    11. }
    12. QFileInfo QuoInfo(ftpfile);
    13. long fzize = QuoInfo.size(); /* file size */
    14. /* ftpfile = c:\image.jpg / remotefile = ftp://username:pass@host.com:21/path/image.jpg /path/ related to ftp login */
    15. /* url = ftp:{login}:{password}@{host}:21{dated dir}{data remotefile} */
    16. QByteArray lop = ftpfile.toAscii();
    17. QByteArray ba = remotefile.toAscii();
    18. char *localfile = lop.data();
    19. FILE *putfile;
    20. putfile = fopen(localfile, "rb"); /* binary image! */
    21. char *url = ba.data();
    22. /*
    23.   qDebug() << "### localfile " << localfile;
    24.   qDebug() << "### Puturl " << url;
    25.   */
    26. /* handy for debugging: see *everything* that goes on */
    27. /* curl_easy_setopt( curl_handle , CURLOPT_VERBOSE, 1 ) ; */ /* open to debug to sea actions */
    28. // target url:
    29. curl_easy_setopt( curl_handle , CURLOPT_URL, url ) ;
    30. // no progress bar:
    31. curl_easy_setopt(curl_handle,CURLOPT_NOPROGRESS , 1 ) ;
    32. // use passive FTP, if it's available
    33. curl_easy_setopt(curl_handle,CURLOPT_FTP_USE_EPSV , 1 ) ;
    34. curl_easy_setopt(curl_handle,CURLOPT_TIMEOUT , 15 ); /* put on server config qwidget */
    35. curl_easy_setopt(curl_handle,CURLOPT_UPLOAD , 1 );
    36. curl_easy_setopt(curl_handle,CURLOPT_INFILE , putfile );
    37. curl_easy_setopt(curl_handle,CURLOPT_INFILESIZE , fzize );
    38. /* make action */
    39. if (curl_easy_perform(curl_handle)==CURLE_OK) {
    40. curl_easy_cleanup( curl_handle );
    41. curl_global_cleanup();
    42. return true;
    43. } else {
    44. return false;
    45. }
    46. }
    47.  
    48.  
    49.  
    50.  
    51.  
    52. // yes, it's "mkd" and not "mkdir" (many clients translate it for you)
    53. // see RFC 959 for details:
    54. //
    55. // http://www.ietf.org/rfc/rfc959.txt
    56. //
    57. // NOTE: creating/changing to the dir isn't necessary here; it's done as
    58. // part of the example of using CURLOPT_QUOTE. You could simply have
    59. // specified CURLOPT_FTP_CREATE_MISSING_DIRS.
    60.  
    61. commandBuf << "mkd " << remotePath << std::flush ;
    62. const std::string mkdirCommand = commandBuf.str() ;
    63. commands = curl_slist_append( commands , mkdirCommand.c_str() ) ;
    64.  
    65. // NOTE: cURL will try to change dirs (based on the URL)
    66. // from the present directory (by default, your home dir
    67. // on the remote system).
    68.  
    69. // To upload data to an absolute path on the remote host,
    70. // you must change to the root dir first.
    71. commands = curl_slist_append( commands , "cwd /" ) ;
    72. // from here, cURL will "cwd tmp" and "cwd {timestamp dir}"
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QDirModel - show directory ".."?
    By jamd in forum Qt Programming
    Replies: 3
    Last Post: 23rd April 2009, 20:51
  2. create file in another directory
    By raphaelf in forum Qt Programming
    Replies: 3
    Last Post: 16th February 2006, 11:04
  3. Am I the only one with "make" error ?
    By probine in forum Installation and Deployment
    Replies: 1
    Last Post: 13th February 2006, 13:54
  4. saving into a directory
    By Bahar in forum Qt Programming
    Replies: 5
    Last Post: 7th February 2006, 17:19
  5. QT Service and working directory
    By Lele in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2006, 12:46

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.