Results 1 to 8 of 8

Thread: QFtp report error

  1. #1
    Join Date
    Jan 2014
    Posts
    7
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default QFtp report error

    Hi everyone.

    I want to upload a file and sometimes this "upload" doesn't work fine. I have tried to catch the error but I haven't succeeded.I just need that when the file doesn't upload the function upload returns FALSE.

    That is my code
    Qt Code:
    1. void FileUploader::ftpCommandFinished(int id, bool error){
    2. if (myUploader->currentCommand() == QFtp::ConnectToHost) {
    3. if (error){
    4. printf("upload file resume ERROR: %s\n",myUploader->errorString().toAscii().data());
    5. myUploader->abort();
    6. myUploader->deleteLater();
    7. myUploader = 0;
    8. return;
    9. }else
    10. //if (id == login)
    11. myFile->remove("data/Tests/" + m_fileName +".csv");
    12. /*else if (id == connectToHost)
    13. printf("connectToHost finished\n");
    14. else if (id == get)
    15. printf("get finished\n");*/
    16. }else if (myUploader->currentCommand() == QFtp::Login){
    17. if (error){
    18. printf("upload file resume ERROR: %s\n",myUploader->errorString().toAscii().data());
    19. myUploader->abort();
    20. myUploader->deleteLater();
    21. myUploader = 0;
    22. return;
    23. }else
    24. //if (id == login)
    25. myFile->remove("data/Tests/" + m_fileName +".csv");
    26. }else if (myUploader->currentCommand() == QFtp::Put){
    27. if (error){
    28. printf("upload file resume ERROR: %s\n",myUploader->errorString().toAscii().data());
    29. myUploader->abort();
    30. myUploader->deleteLater();
    31. myUploader = 0;
    32. return;
    33. }else
    34. //if (id == login)
    35. myFile->remove("data/Tests/" + m_fileName +".csv");
    36. }
    37. }
    38.  
    39. bool FileUploader::upload(QString fileName,accountInfoSettings* myAccountInfoSettings){
    40.  
    41. myUploader = new QFtp();
    42. /*bool error = FileUploader::zip(fileName);
    43. if(!error){
    44.  
    45. printf("error del bueno\n");
    46. fflush(stdout);
    47. }
    48.  
    49. printf("Zip file created\n");
    50. fflush(stdout);*/
    51.  
    52. myFile = new QFile("data/Tests/" +fileName); //.remove(".csv")+".zip");
    53.  
    54. m_fileName = fileName;
    55. myFile->open(QFile::ReadOnly);
    56. connect(myUploader, SIGNAL(commandFinished(int,bool)), this, SLOT(ftpCommandFinished(int,bool)));
    57. /*myUploader->connectToHost("www.at4wireless.com",21); myUploader->login("demo_at4","H3mEwwUC");*/
    58. myUploader->connectToHost(myAccountInfoSettings->getUrl(),21);
    59. myUploader->login(myAccountInfoSettings->getUser(),myAccountInfoSettings->getPassword());
    60. myUploader->put(myFile,fileName);
    61. printf("Put\n"); fflush(stdout);
    62.  
    63. if (QFtp::UnknownError == myUploader->error() || QFtp::HostNotFound == myUploader->error() ||
    64. QFtp::ConnectionRefused == myUploader->error() || QFtp::NotConnected == myUploader->error())
    65. return false;
    66. else return true;
    67. }
    To copy to clipboard, switch view to plain text mode 

    Any help is welcome

    Thanks again, Carmen.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QFtp report error

    QFtp works asynchronously, so it hasn't started working yet when you check before returning.

    Most methods to fake blocking behavior have unwanted side effects, so the best way would be not to rely on that but start the upload and get its result via signal.

    Which version of Qt are we talking about here, btw?

    Cheers,
    _

  3. #3
    Join Date
    Jan 2014
    Posts
    7
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: QFtp report error

    Hi. Firstly thank you to answer me

    I'm not sure about it but I think that the Qt version is 4.8.

    I have tried too that:

    Qt Code:
    1. void FileUploader::ftpCommandFinished(int id, bool error){
    2. if (error){
    3. printf("upload file resume ERROR: %s\n",myUploader->errorString().toAscii().data());
    4. myUploader->abort();
    5. myUploader->deleteLater();
    6. myUploader = 0;
    7. return;
    8. }else
    9. myFile->remove("data/Tests/" + m_fileName +".csv");
    10. }
    11.  
    12. bool FileUploader::upload(QString fileName,accountInfoSettings* myAccountInfoSettings){
    13.  
    14. myUploader = new QFtp();
    15.  
    16. myFile = new QFile("data/Tests/" +fileName); //.remove(".csv")+".zip");
    17.  
    18. m_fileName = fileName;
    19. myFile->open(QFile::ReadOnly);
    20. connect(myUploader, SIGNAL(commandFinished(int,bool)), this, SLOT(ftpCommandFinished(int,bool)));
    21.  
    22. if (QFtp::UnknownError == myUploader->error() || QFtp::HostNotFound == myUploader->error() ||
    23. QFtp::ConnectionRefused == myUploader->error() || QFtp::NotConnected == myUploader->error())
    24. return false;
    25. myUploader->connectToHost(myAccountInfoSettings->getUrl(),21);
    26. if (QFtp::UnknownError == myUploader->error() || QFtp::HostNotFound == myUploader->error() ||
    27. QFtp::ConnectionRefused == myUploader->error() || QFtp::NotConnected == myUploader->error())
    28. return false;
    29. myUploader->login(myAccountInfoSettings->getUser(),myAccountInfoSettings->getPassword());
    30. if (QFtp::UnknownError == myUploader->error() || QFtp::HostNotFound == myUploader->error() ||
    31. QFtp::ConnectionRefused == myUploader->error() || QFtp::NotConnected == myUploader->error())
    32. return false;
    33. myUploader->put(myFile,fileName);
    34. printf("Put\n"); fflush(stdout);
    35.  
    36. if (QFtp::UnknownError == myUploader->error() || QFtp::HostNotFound == myUploader->error() ||
    37. QFtp::ConnectionRefused == myUploader->error() || QFtp::NotConnected == myUploader->error())
    38. return false;
    39. else return true;
    40. }
    To copy to clipboard, switch view to plain text mode 

    And it didn't work either.
    Last edited by Carmengg; 4th February 2014 at 08:59.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QFtp report error

    I'll try to explain that a bit better:

    When you do any of the QFtp commands, e.g.
    Qt Code:
    1. myUploader->connectToHost(...)
    To copy to clipboard, switch view to plain text mode 
    they will return immediately with an identifier value for that operation. The operation itself is executed asynchronously, its result is signalled via commandFinished().
    So checking an error code or state right after the call is more or less without meaning (will return the state of the QFtp class at that time, not related to the new operation at all).

    What you have here is a sequence of such operations being scheduled for execution, they will be executed after you return from upload().

    If you want to know when the put operation has finished, you need to remember the operation identifier returned by put() and when that id comes along in ftpCommandFinished() then you are done.
    E.g. you can then emit your own signal delete the file and ftp instances, etc.

    Cheers,
    _

  5. #5
    Join Date
    Jan 2014
    Posts
    7
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: QFtp report error

    Sorry. I'm still lost.

    Could you give me a example with code?

    Thanks

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QFtp report error

    Which part is it that you have a problem with?

    Storing the request id?

    Qt Code:
    1. void FileUploader::upload(QString fileName,accountInfoSettings* myAccountInfoSettings){
    2.  
    3. myUploader = new QFtp();
    4. connect(myUploader, SIGNAL(commandFinished(int,bool)), this, SLOT(ftpCommandFinished(int,bool)));
    5.  
    6. myFile = new QFile("data/Tests/" +fileName); //.remove(".csv")+".zip");
    7.  
    8. m_fileName = fileName;
    9. myFile->open(QFile::ReadOnly);
    10.  
    11. myUploader->connectToHost(myAccountInfoSettings->getUrl(),21);
    12. myUploader->login(myAccountInfoSettings->getUser(),myAccountInfoSettings->getPassword());
    13.  
    14. m_uploadId = myUploader->put(myFile,fileName); // remember ID of upload operation
    15. }
    To copy to clipboard, switch view to plain text mode 
    Or checking the ID?
    Qt Code:
    1. void FileUploader::ftpCommandFinished(int id, bool error){
    2. if (error){
    3. printf("upload file resume ERROR: %s\n",myUploader->errorString().toAscii().data());
    4. myUploader->abort();
    5. }else if (id == m_uploadId) // upload operation finished
    6. myFile->remove("data/Tests/" + m_fileName +".csv");
    7.  
    8. myUploader->deleteLater();
    9. myUploader = 0;
    10.  
    11. delete myFile;
    12. myFile = 0;
    13. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2014
    Posts
    7
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: QFtp report error

    Well.
    I need to know if any of this operations has finished succesfull.
    connnectToHost, login, put
    If something wrong happend the function "FileUploader" has to return false and then my app says that the file has not been uploaded. Untill now each time my app try to upload a file says that file has been uploaded correctly but it's not true. For example sometimes I have not wifi signal

    I do not care which of them has missed. If any functions didn't work I only need to know that as a result the file didn't upload.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QFtp report error

    Your options are one signal carrying a state information or two signal

    Fo example one signal with state:
    Qt Code:
    1. void FileUploader::ftpCommandFinished(int id, bool error){
    2. if (error){
    3. printf("upload file resume ERROR: %s\n",myUploader->errorString().toAscii().data());
    4. myUploader->abort();
    5. emit finished(false);
    6. }else if (id == m_uploadId){ // upload operation finished
    7. myFile->remove("data/Tests/" + m_fileName +".csv");
    8. emit finished(true);
    9. }
    10.  
    11. myUploader->deleteLater();
    12. myUploader = 0;
    13.  
    14. delete myFile;
    15. myFile = 0;
    16. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. FileUploader *uploader = new FileUploader(this);
    2. connect(uploader, SIGNAL(finished(bool)), this, SLOT(onUploadFinished(bool)));
    3.  
    4. uploader->upload(...);
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void SomeClass::onUploadFinished(bool success)
    2. {
    3. FileUploader *uploader = qobject_cast<FileUploader*>(sender());
    4.  
    5. // react on success
    6.  
    7. uploader->deleteLater();
    8. }
    To copy to clipboard, switch view to plain text mode 

    You can of course re-use the same uploader instance, have the filename in the signal, etc.

    Alternatively you could look at QNetworkAccessManage:ut()

    Cheers,
    _

Similar Threads

  1. Replies: 7
    Last Post: 20th March 2011, 22:07
  2. Replies: 0
    Last Post: 17th November 2009, 20:59
  3. QT report
    By triperzz in forum Qt Programming
    Replies: 3
    Last Post: 25th February 2008, 17:58
  4. Replies: 0
    Last Post: 23rd September 2007, 11:54
  5. error report
    By Rekha in forum Newbie
    Replies: 1
    Last Post: 14th August 2006, 12:11

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.