Results 1 to 5 of 5

Thread: Uploading of Entire Directory(with subdirectories and files) - Please Help

  1. #1
    Join Date
    Mar 2010
    Posts
    8
    Thanks
    7
    Qt products
    Qt4

    Default Uploading of Entire Directory(with subdirectories and files) - Please Help

    Hi, this is the problem i v been struggling with now for a while and i kinda feel desparate. I am trying to upload directory with all of its contest in my small ftp client app but just when it looks as everything works some small bug slips in. Here is my code so please take a look

    Qt Code:
    1. void FtpWindow::upload_slot(){
    2.  
    3. QString name = homefileList->currentItem()->text(0); // Here i get dir name from the treewidgetlist selected item
    4.  
    5.  
    6. QString filepath = path.absoluteFilePath(name); // Since path is changed each time i click on a directory in treewidget and enter it, here is how i get the path of directory i want to upload
    7.  
    8.  
    9.  
    10. if (!isHomeDir.value(name)) {uploadFile(filepath, name);} // this is just checking if its dir or file
    11.  
    12. else { uploadDirectory(filepath, name); }
    13.  
    14. ftp->list();
    15. }
    16.  
    17.  
    18. void FtpWindow::uploadFile(const QString& filePath, const QString &name)
    19.  
    20. {
    21.  
    22. QFile *file = new QFile(filePath);
    23. if ( !file->open(QIODevice::ReadOnly) ) {
    24. QMessageBox::critical( this, tr("Upload error"),
    25. tr("Can't open file '%1' for reading.").arg(filePath) );
    26. delete file;
    27. return;
    28. }
    29. ftp->put(file, name);
    30. }
    31.  
    32. void FtpWindow::uploadDirectory(const QString& dirPath, const QString &name)
    33.  
    34. {
    35.  
    36. QDir targetDir(dirPath);
    37.  
    38.  
    39.  
    40. uploadPath = currentPath;
    41. qDebug() << uploadPath;
    42.  
    43. ftp->mkdir(name);
    44. ftp->cd(name);
    45.  
    46.  
    47.  
    48.  
    49. QDirIterator it(targetDir, QDirIterator::Subdirectories);
    50.  
    51.  
    52. while (it.hasNext()) {
    53.  
    54. QFileInfo info = it.next();
    55.  
    56.  
    57.  
    58.  
    59. if(it.fileName() == "." | it.fileName() == ".." | it.fileName() == "/") continue;
    60.  
    61. if(info.isDir()) {
    62. info.dir().absolutePath();
    63.  
    64.  
    65. QString dirpath = info.absoluteFilePath().remove(0, info.absoluteFilePath().indexOf('/'+ name));
    66.  
    67. QString parentdir = uploadPath + dirpath.left(dirpath.lastIndexOf('/'));
    68. qDebug() << dirpath << "is dir and it's parent dir is " << parentdir ;
    69.  
    70. ftp->cd(parentdir);
    71. ftp->mkdir(info.baseName());
    72.  
    73.  
    74. }
    75.  
    76. else {
    77.  
    78. QString fajlovi = info.absoluteFilePath().remove(0, info.absoluteFilePath().indexOf('/'+ name));
    79.  
    80. QString parentdir = uploadPath + fajlovi.left(fajlovi.lastIndexOf('/'));
    81. // ftp->cd(parentdir);
    82. qDebug() << fajlovi << "is file and its parent dir is " << parentdir ;
    83. ftp->cd(parentdir);
    84. uploadFile(info.absoluteFilePath(), info.baseName());
    85.  
    86. }
    87.  
    88.  
    89. }
    90.  
    91.  
    92. }
    To copy to clipboard, switch view to plain text mode 

    Basically what uploadirectory function does is creates the dir of the main dir(i find the name of it in the top of code as described) inside ftp server, then i use qiterator to go trough every dir or file and if its dir i get its parent dir, ftp->cd inside of it then upload the file or just create dir if its dir.

    The problem is this sometimes doesnt work because getting parent dir with this QString functions is as i experienced unreliable, what i really need is either fix that would make this work or another reliable way to handle uploading of entire dirs, please help me finally solve this, if anyone knows a source that explains how this is done maybe?

    I am sick of this problem totally

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Uploading of Entire Directory(with subdirectories and files) - Please Help

    Any particular reason you didn't use a recursive approach? Something like:
    Qt Code:
    1. void upload(const QString &localPath)
    2. {
    3. QFileInfo localInfo(localPath);
    4. if (localInfo.isFile()) {
    5. qDebug() << "FTP put file" << localInfo.fileName();
    6. }
    7. else if (localInfo.isDir()) {
    8. qDebug() << "FTP mkdir" << localInfo.fileName();
    9.  
    10. QDirIterator it(localPath,
    11. QDir::NoDotAndDotDot | QDir::Dirs | QDir::Files);
    12. if (it.hasNext()) {
    13. // only bother if there is something to do; the remote chdir() is expensive
    14. qDebug() << "FTP cd" << localInfo.fileName();
    15.  
    16. while (it.hasNext()) {
    17. QFileInfo info = it.next();
    18. upload(info.absoluteFilePath());
    19. }
    20.  
    21. qDebug() << "FTP cd ..";
    22. }
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 
    called like:
    Qt Code:
    1. qDebug() << "FTP cd remotepath";
    2. upload("/tmp/main.cpp"); // will create remotepath/main.cpp file on remote
    3.  
    4. qDebug() << "FTP cd remotepath2";
    5. upload("/tmp"); // will create a remotepath2/tmp folder tree on remote
    To copy to clipboard, switch view to plain text mode 

    You will need to insert the FTP commands, check for errors, test etc.

  3. The following user says thank you to ChrisW67 for this useful post:

    msimurin (13th July 2010)

  4. #3
    Join Date
    Mar 2010
    Posts
    8
    Thanks
    7
    Qt products
    Qt4

    Default Re: Uploading of Entire Directory(with subdirectories and files) - Please Help

    Thank you ChrisW67 for taking time to help, i tried recursive approach before but i didnt do it right so i switched to this. I will try what you suggested now and see how it goes...

  5. #4
    Join Date
    Mar 2010
    Posts
    8
    Thanks
    7
    Qt products
    Qt4

    Default Re: Uploading of Entire Directory(with subdirectories and files) - Please Help

    Ok, after testing this it seems everything works solid and well, you saved me ChrisW67, there is just one small problem, it takes 45 minutes to upload bunch of files(total of 19mb) which is really slow for my upload connection. By watching netlimiter and monitoring upload speeds it seems it is always low speed for some reason, like from 3-20kB average or less. I can normally pull 50kB.

    is there anything i could do about this?

  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Uploading of Entire Directory(with subdirectories and files) - Please Help

    The network speed is unrelated to code for walking the directory tree. It will only try to send one file at a time and there will be almost no time between finishing a file upload and issuing the next FTP command. The speed onto the wire could be constrained by the FTP code you are using, external users of the link, firewalls, the remote end... Try sending the same set of files using another FTP client (e.g. FileZilla) to get an unbiased comparison figure.

    Make sure the FTP connection is not reporting error or even being dropped/re-established for each file.

  7. The following user says thank you to ChrisW67 for this useful post:

    msimurin (15th July 2010)

Similar Threads

  1. Searching files from directories & its subdirectories
    By ashukla in forum Qt Programming
    Replies: 4
    Last Post: 12th April 2014, 12:57
  2. How to display entire directory(Filesystem)
    By deepakn in forum Newbie
    Replies: 11
    Last Post: 10th November 2009, 09:58
  3. directory and files
    By rmagro in forum Qt Programming
    Replies: 2
    Last Post: 16th September 2008, 13:40
  4. uploading files to HTTP!!
    By Raajesh in forum Qt Programming
    Replies: 2
    Last Post: 24th June 2008, 22:00
  5. QFtp : Uploading a directory
    By smshamim in forum Qt Programming
    Replies: 2
    Last Post: 31st August 2006, 12:06

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.