×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    21

    Upload file problem

    Hello

    I want to upload a file to ftp server, and delete it when it has uploaded to server success. I use the following code to implement, but the size of uploaded file always is zero. I think the file will be delete before upload to server. So how could I solve this problem ?? Thanks a lot....

    =================================================================================
    bool FTPTest::UploadFile(){

    QProgressDialog * progress = new QProgressDialog(tr("Uploading file..."),tr("Cancel"),0,0,this,0);
    mFileName = "file.txt";
    progress->show();
    m_ftpError = false;

    connect( ftp, SIGNAL(dataTransferProgress(int,int)),progress, SLOT(setProgress(int,int)) );

    connect( ftp, SIGNAL(commandFinished(int,bool)), progress, SLOT(reset()));

    connect( ftp, SIGNAL(done (bool)), this, SLOT(ftperror(bool) ));

    connect( progress, SIGNAL(cancelled()), ftp, SLOT(abort()) );

    ftp->connectToHost("xxx.xxx.xxx.xxx");
    ftp->login("Account","Password");

    file = new QFile("/DATA/" + mFileName);
    ftp->put( file, mFileName);

    progress->exec();

    file->close();
    file->remove();

    if(m_ftpError ) // it seems can't wait the ftp process.
    return false;

    return true;
    }

    void FTPTest::ftperror(bool error){

    // How could I let above function know if error occur.
    if(error)
    m_ftpError =true;
    }
    ====================================================================
    Last edited by jwzeng0312; 2010-06-27 at 14:51.

  2. #2
    Registered User
    Join Date
    Jun 2010
    Posts
    21

    Re: Upload file problem

    connect(ui.m_ButtonUpload, SIGNAL(clicked()), this, SLOT(UploadFile()));

    As I put the upload button down, phone will show the searching WLAN dialog. How could I make sure that WLAN is connected before run the upload file function ??

  3. #3
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: Upload file problem

    QFtp (which I assume you're using from the terminology) is deprecated. Use QtNetwork instead. To tell if you have a connection & manage the connection use QtMobility Bearer Management.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    21

    Re: Upload file problem

    Thanks for your re[lay.

    Could you give details information or provide a example ?? Does QFtp has some problem ??

    Thanks a lot...

  5. #5
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: Upload file problem

    This is just a hack I used to test some concepts, and with an early version of Mobility:
    Code:
    #ifndef TESTMOBILITY_H
    #define TESTMOBILITY_H
    
    #include <QtGui/QMainWindow>
    #include "ui_TestMobility.h"
    #include <QNetworkSession>
    #include <QList>
    #include <QSslError>
    #include <QSemaphore>
    
    class QNetworkAccessManager;
    class QNetworkRequest;
    class QNetworkReply;
    
    using namespace QtMobility;
    
    class TestMobility : public QMainWindow {
    	Q_OBJECT
    
    public:
    	TestMobility(QWidget *parent = 0);
        ~TestMobility();
    
    signals:
       void signalNetworkingCancelled();
        
    private slots:
    	void slotDelayedInit();
    	void slotNetworkSessionOpened();
    	void slotCheckFinished();
    	void slotRequestFinished(QNetworkReply* reply);
    	void slotNetworkSessionError(QNetworkSession::SessionError error);
    	void slotSslError(QNetworkReply* reply, const QList<QSslError>& errors);
    	
    private:
        Ui::TestMobility ui;
        QNetworkAccessManager* mNetworkAccessManager;
        QNetworkSession* mSession;
        QNetworkReply* mReply;
        QString mImei;
        QSemaphore mSemaphore;
        bool mHandled;
        
        void requestFinished(QNetworkReply* reply);
    };
    
    #endif // TESTMOBILITY_H
    Code:
    #include "TestMobility.h"
    #include "secrets.h"
    #include <QtNetwork>
    #include <QNetworkConfigurationManager>
    #include <QSystemDeviceInfo>
    #include <QNetworkAccessManager>
    #include <QNetworkSession>
    #include <QNetworkRequest>
    #include <QNetworkReply>
    #include <QSslCertificate>
    #include <QCryptographicHash>
    #include <QByteArray>
    #include <QList>
    #include <QVariant>
    #include <QMessageBox>
    #include <QTimer>
    
    using namespace QtMobility;
    
    TestMobility::TestMobility(QWidget *parent)
        : QMainWindow(parent)
    {
    	ui.setupUi(this);
    	
    	QNetworkConfigurationManager manager;
    	QNetworkConfigurationManager::Capabilities cap = manager.capabilities();
    	bool startStop = cap & QNetworkConfigurationManager::CanStartAndStopInterfaces;
    	bool dirCon = cap & QNetworkConfigurationManager::DirectConnectionRouting;
    	bool sysSesSup = cap & QNetworkConfigurationManager::SystemSessionSupport;
    	bool alr = cap & QNetworkConfigurationManager::ApplicationLevelRoaming;
    	bool forced = cap & QNetworkConfigurationManager::ForcedRoaming;
    	bool dataStat = cap & QNetworkConfigurationManager::DataStatistics;
    	
    	QString text; 
    	// Get IMEI string (International Mobile Equipment Identity)
    	QSystemDeviceInfo info;
    	mImei = info.imei();
    #ifdef __WINS__
    	if (mImei == "000000000000000") {
    		mImei = "354182030698607";
    	}
    #endif
    	
    	mNetworkAccessManager = new QNetworkAccessManager(this);
    	bool ok;
    	ok = connect(mNetworkAccessManager, SIGNAL(finished(QNetworkReply*)),
    			this, SLOT(slotRequestFinished(QNetworkReply*)));
    	Q_ASSERT(ok);
    	ok = connect(mNetworkAccessManager, SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError>&)),
    			this, SLOT(slotSslError(QNetworkReply*, const QList<QSslError>&)));
    	Q_ASSERT(ok);
    	Q_UNUSED(ok);
    	
    	QTimer::singleShot(0, this, SLOT(slotDelayedInit()));
    }
    
    void TestMobility::slotDelayedInit() {
    	QNetworkConfigurationManager manager;
    	const bool canStartIAP = (manager.capabilities()
    							  & QNetworkConfigurationManager::CanStartAndStopInterfaces);
    	
    	// Note:  Should be calling updateConfigurations here.  See QNetworkConfigurationManager::allConfigurations writeup.
    
    	QNetworkConfiguration cfg = manager.defaultConfiguration();
    	if (!cfg.isValid() || (!canStartIAP && cfg.state() != QNetworkConfiguration::Active)) {
    		QMessageBox::information(this, tr("TestMobility"), tr("Available Access Points not found."));
    		return;
    	}
    	
    	if (cfg.type() == QNetworkConfiguration::UserChoice) {
    		QNetworkConfiguration::StateFlags flags = QNetworkConfiguration::Discovered | QNetworkConfiguration::Active;
    		QList<QNetworkConfiguration> allCfg = manager.allConfigurations(flags);
    		// Just take the first one
    		if (allCfg.count() > 0) {
    			// cfg = allCfg.at(0);  // Works, but like the feedback of the prompt
    		}
    	}
    
    	mSession = new QNetworkSession(cfg, this);
    	bool ok = connect(mSession,
    			SIGNAL(error(QNetworkSession::SessionError)),
    			this,
    			SLOT(slotNetworkSessionError(QNetworkSession::SessionError)));
    	Q_ASSERT(ok);
    	ok = connect(mSession, SIGNAL(opened()), this, SLOT(slotNetworkSessionOpened()));
    	Q_ASSERT(ok);
    	ok = connect(this, SIGNAL(signalNetworkingCancelled()), qApp, SLOT(quit()));
    	Q_ASSERT(ok);
    	Q_UNUSED(ok);
    	mSession->open();
    }
    
    void TestMobility::slotNetworkSessionOpened() {
    	// Construct HTTP request
    	QString user("dhicks");
    	sigString.append(appId);
    	sigString.append(":");
    	sigString.append(user);
    	QByteArray byteResult = QCryptographicHash::hash(sigString.toAscii(), QCryptographicHash::Md5);
    	QString strResult(byteResult.toHex());
    	if (strResult.count() % 2 != 0) {
    		strResult = '0' + byteResult;
    	}
    	strResult = strResult.toLower();
    	
    	// Construct JSON string
    	QString json;
    	json.append("&json=");
    	json.append("{\"filter\":{},");
    	json.append("\"content\":{");
    	json.append("\"patients\":[");
    	json.append("{\"interchangeSerial\":\"Patient12345\",");
    	json.append("\"interchangePatientId\":3456,");
    	json.append("\"display\":\"William\",");
    	json.append("\"deleted\":0,");
    	json.append("\"timezone\":\"CST\"}");
    	json.append("]}");
    	json.append("}");
    	
    	// Construct request string
    	QString urlString;
    	urlString.append(ServerUrl);
    	urlString.append("?user=");
    	urlString.append(user);
    	urlString.append(strResult);
    	urlString.append(json);
    	QUrl url(urlString);
    	
    	QNetworkRequest request(url);
    	// request.setSslConfiguration(QSslConfiguration::defaultConfiguration());
    	if (QSslConfiguration::defaultConfiguration().isNull()) {
    		bool nullDefault = true;
    	}
    	QByteArray data;
    	mReply = mNetworkAccessManager ->post(request, data);
    	mHandled = false;
    	mSemaphore.release();
    	QTimer::singleShot(10000, this, SLOT(slotCheckFinished()));
    }
    
    void TestMobility::slotCheckFinished() {
    	mSemaphore.acquire();
    	// Note that we must check mHandled first, since mReply could have been deleted
    	if (mHandled == false && mReply ->isFinished()) {
    		mHandled = true;
    		mSemaphore.release();
    		requestFinished(mReply);
    	}
    	else {
    		mSemaphore.release();
    	}
    }
    
    void TestMobility::slotRequestFinished(QNetworkReply* reply) {
    	mSemaphore.acquire();
    	if (mHandled == false) {
    		mHandled == true;
    		mSemaphore.release();
    		requestFinished(reply);
    	}
    	else {
    		mSemaphore.release();
    	}
    }
    
    void TestMobility::requestFinished(QNetworkReply* reply) {
    	QNetworkRequest request = reply ->request();
    	QNetworkReply::NetworkError error = reply ->error();
    	if (error != QNetworkReply::NoError) {
    		Q_ASSERT(false);
    	}
    	int status = reply ->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    	QVariant encrypted = reply ->attribute(QNetworkRequest::ConnectionEncryptedAttribute);
    	QSslConfiguration config = request.sslConfiguration();
    	QSslCertificate cert = config.peerCertificate();
    	ui.mLabel2 ->setText(QString("HTTP status code = %1").arg(status));
    	// Get return data
    	QByteArray result;
    	result = reply ->readAll();
    	if (result.count() == 0) {
    		ui.mLabel ->setText("Zero length result");
    	}
    	else {
    		QString resultString(result);
    		// Enable better word wrap
    		resultString = resultString.replace(':', ": ");
    		resultString = resultString.replace(',', ", ");
    		resultString.append("\nEncrypted = ");
    		resultString.append(encrypted.toBool() ? "true" : "false");
    		resultString.append("\nSSL config is null = ");
    		resultString.append(config.isNull() ? "true" : "false");
    		resultString.append("\nProtocol = ");
    		resultString.append(QString::number(config.protocol()));
    		resultString.append("\nCert serial number = ");
    		resultString.append(QString(cert.serialNumber()));
    		resultString.append("\nCert expire date = ");
    		resultString.append(cert.expiryDate().toString());
    		ui.mLabel ->setWordWrap(true);
    		ui.mLabel ->setText(resultString);
    	}
    	reply ->deleteLater();
    }
    
    void TestMobility::slotSslError(QNetworkReply* reply, const QList<QSslError>& errors) {
    	QString message;
    	message.append("SSL error");
    	for (int i = 0; i < errors.count(); i++) {
    		message.append("\n");
    		message.append(errors.at(i).errorString());
    	}
    }
    
    void TestMobility::slotNetworkSessionError(QNetworkSession::SessionError error) {
        if (error == QNetworkSession::UnknownSessionError) {
            if (mSession->state() == QNetworkSession::Connecting) {
                QMessageBox msgBox(qobject_cast<QWidget *>(parent()));
                msgBox.setText("This application requires network access to function.");
                msgBox.setInformativeText("Press Cancel to quit the application.");
                msgBox.setIcon(QMessageBox::Information);
                msgBox.setStandardButtons(QMessageBox::Retry | QMessageBox::Cancel);
                msgBox.setDefaultButton(QMessageBox::Retry);
                int ret = msgBox.exec();
                if (ret == QMessageBox::Retry) {
                    QTimer::singleShot(0, mSession, SLOT(open()));
                } else if (ret == QMessageBox::Cancel) {
                    emit signalNetworkingCancelled();
                }
            }
        } else if (error == QNetworkSession::SessionAbortedError) {
            QMessageBox msgBox(qobject_cast<QWidget *>(parent()));
            msgBox.setText("Out of range of network.");
            msgBox.setInformativeText("Move back into range and press Retry, or press Cancel to quit the application.");
            msgBox.setIcon(QMessageBox::Warning);
            msgBox.setStandardButtons(QMessageBox::Retry | QMessageBox::Cancel);
            msgBox.setDefaultButton(QMessageBox::Retry);
            int ret = msgBox.exec();
            if (ret == QMessageBox::Retry) {
                QTimer::singleShot(0, mSession, SLOT(open()));
            } else if (ret == QMessageBox::Cancel) {
                emit signalNetworkingCancelled();
            }
        }
    }
    
    
    TestMobility::~TestMobility()
    {
    
    }

Similar Threads

  1. File Upload using Http POST problem
    By chandran.biju007 in forum Symbian
    Replies: 11
    Last Post: 2009-08-17, 07:29
  2. HTTP file upload problem
    By Asif Nazir in forum Symbian Networking & Messaging (Closed)
    Replies: 5
    Last Post: 2008-03-26, 08:09
  3. URGENT: File upload problem on Nokia 6680
    By eugeneleck in forum Mobile Java General
    Replies: 4
    Last Post: 2006-02-13, 16:01
  4. Upload file Problem with XHTML
    By ronny60299 in forum Browsing and Mark-ups
    Replies: 1
    Last Post: 2004-12-08, 07:55

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×