Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QHttpMultiPart file upload
Qt 6.11 is out! See what's new in the release blog

QHttpMultiPart file upload

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 5.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 4 Offline
    4 Offline
    4j1th
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      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)?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      4 1 Reply Last reply
      2
      • VRoninV VRonin

        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)?

        4 Offline
        4 Offline
        4j1th
        wrote on last edited by
        #3

        @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

        1 Reply Last reply
        0
        • 4 Offline
          4 Offline
          4j1th
          wrote on last edited by
          #4

          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
          
          1 Reply Last reply
          0
          • 4 Offline
            4 Offline
            4j1th
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • 4 Offline
              4 Offline
              4j1th
              wrote on last edited by 4j1th
              #6

              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
              
              
              1 Reply Last reply
              0
              • 4 Offline
                4 Offline
                4j1th
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved