QHttpMultiPart file upload



  • Hi, I am trying to upload file from the Qt app to the server, from QHttpMultiPart doc copied this code

    void FileUploader::postProcesser3()
    {
        qDebug() << "uploading 3";
    
        QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    
        QHttpPart textPart;
        textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"text\""));
        textPart.setBody("my text");
    
        QHttpPart imagePart;
        imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
        imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"image\"; filename=\"1.jpg\""));
        QFile *file = new QFile("1.jpg");
        qDebug() << file->open(QIODevice::ReadOnly);
        imagePart.setBodyDevice(file);
        file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
    
        multiPart->append(textPart);
        multiPart->append(imagePart);
    
        QUrl url("http://localhost/medialinks/ci/index.php/data/fileupload_test/");
        QNetworkRequest request(url);
    
        //QNetworkAccessManager manager;
        QNetworkAccessManager * manager = new QNetworkAccessManager();
        QNetworkReply *reply = manager->post(request, multiPart);
        multiPart->setParent(reply); // delete the multiPart with the reply
    
        // here connect signals etc.
        connect(manager, SIGNAL(finished(QNetworkReply*)), SLOT(replyFinished(QNetworkReply*)));
        connect(reply, SIGNAL(uploadProgress(qint64,qint64)), this, SLOT(SlotSetProgressLevel(qint64, qint64)));
    
        connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
                 this, SLOT  (uploadError(QNetworkReply::NetworkError)));
    }
    
    void FileUploader::uploadError(QNetworkReply::NetworkError)
    {
        qDebug() << "error";
    }
    
    void FileUploader::SlotSetProgressLevel(qint64, qint64)
    {
        qDebug() << "uploading progress";
    }
    
    void FileUploader::replyFinished(QNetworkReply* reply)
    {
        qDebug() << "upload finished";
    
        qDebug() << reply->readAll();
    }
    

    but I don't get any error or finished signal but the file also not uploaded. This is the debugging result

    uploading 3
    true
    qt.network.ssl: QSslSocket: cannot resolve SSLv2_client_method
    qt.network.ssl: QSslSocket: cannot resolve SSLv2_server_method
    

    this is server script it's in codeigniter(php)

    public function fileupload_test()
    	{
    		$this->load->library('upload');
    		$config['upload_path'] = $this->config->item('server_path').'data/event/'.'w_full';
    		$config['file_name'] = "f.jpg";
    
    		$config['allowed_types'] = '*';
    		$this->upload->initialize($config);
    		if(!$this->upload->do_upload('image')){
    
    			//upload error
    			echo "file uploading failed";
    			//var_dump($this->upload->display_errors());
    			
    			// remove created posts
    			//$this->teamdb->remove_unsuccesfull_post($ids);
    			exit;
    		} else {
    
    			// success, add data to database
    			echo 'success';
    		}
    	}
    

    Thanks



  • If you connect to QNetworkAccessManager::sslErrors(QNetworkReply *, const QList<QSslError> &) signal it will probably be more clear what's going on. My guess is that your program can't find OpenSSL. Did you try adding the path to the OpenSSL binaries to the Project->Run Environment in Qt Creator (assuming you use that)?



  • @VRonin, I tried that

    connect(manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)),
                 this, SLOT  (uploadError2(QNetworkReply *, QList<QSslError>)));
    
    
    void FileUploader::uploadError2(QNetworkReply* reply,QList<QSslError> list)
    {
        qDebug() << list << reply->readAll();
    }
    

    debug result

    uploading 3
    true
    qt.network.ssl: QSslSocket: cannot resolve SSLv2_client_method
    qt.network.ssl: QSslSocket: cannot resolve SSLv2_server_method
    

    In my previous project i got this ssl socket error but it work fine, one developer advise me to ignore it, it's some error in libssl https://forum.qt.io/topic/16139/solved-qsslsocket-error-sslv2/3



  • I don't understand what going on with this code but if I move this code to mainwindow class it's work perfectly with the above mentioned error

    qt.network.ssl: QSslSocket: cannot resolve SSLv2_client_method
    qt.network.ssl: QSslSocket: cannot resolve SSLv2_server_method
    


  • This post is deleted!


  • my class deceleration is

    #ifndef FILEUPLOADER_H
    #define FILEUPLOADER_H
    #include <QThread>
    #include <QNetworkReply>
    #include <QList>
    
    
    class FileUploader : public QThread
    {
        Q_OBJECT
    public:
        FileUploader();
    
        void run();
    
    private:
    
        void postProcesser();
        void postProcesser2();
        void postProcesser3();
    
    private slots:
    
        void replyFinished(QNetworkReply *reply);
    
        void SlotSetProgressLevel(qint64, qint64);
    
        void uploadError(QNetworkReply::NetworkError);
    
        void uploadError2(QNetworkReply *reply, QList<QSslError> list);
    };
    
    #endif // FILEUPLOADER_H
    
    


  • sorry guys my mistake I initialize the class like

    Updater update;
    

    so when the function finish executing it also delete my methord

    now I am initialize

    Updater *updatedata = new Updater;
    

    it works, sorry for wasting time


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.