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. Error uploading file on server

Error uploading file on server

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.3k Views
  • 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.
  • D Offline
    D Offline
    developerNancy
    wrote on last edited by developerNancy
    #1

    I am new in Qt and try to select file and upload that file to server. Here is my code

      QString usersfilename = QFileDialog::getOpenFileName(this,
                                             "Select source file",
                                             ".",
                                             "Text files (*.csv);; All files (*.*)");
    
        this->ui->uploadEdit->setText(usersfilename);
    
    
       QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
        QHttpPart imagePart;
        //imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain"));
        imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\"; filename=\"version.txt\""));/* version.tkt is the name on my Disk of the file that I want to upload */
    
        QHttpPart textPart;
        textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"name\""));
        textPart.setBody("filename");
    
       // QString apkLocation = apktextEdit->text();
        QFile *file = new QFile(usersfilename);
        file->open(QIODevice::ReadOnly);
        imagePart.setBodyDevice(file);
        file->setParent(multiPart); 
    
        multiPart->append(textPart);
        multiPart->append(imagePart);
    
        QUrl url("url");
        QNetworkRequest request(url);
    
        QNetworkAccessManager *networkManager= new QNetworkAccessManager;
        QNetworkReply * reply = networkManager->post(request, multiPart);
    

    Can anyone tell me how we select file from directory and upload it to server.

    Thanks in advance.

    jsulmJ 1 Reply Last reply
    0
    • D Offline
      D Offline
      developerNancy
      wrote on last edited by
      #6

      Done Thanks for the help

      1 Reply Last reply
      0
      • D developerNancy

        I am new in Qt and try to select file and upload that file to server. Here is my code

          QString usersfilename = QFileDialog::getOpenFileName(this,
                                                 "Select source file",
                                                 ".",
                                                 "Text files (*.csv);; All files (*.*)");
        
            this->ui->uploadEdit->setText(usersfilename);
        
        
           QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
            QHttpPart imagePart;
            //imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain"));
            imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\"; filename=\"version.txt\""));/* version.tkt is the name on my Disk of the file that I want to upload */
        
            QHttpPart textPart;
            textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"name\""));
            textPart.setBody("filename");
        
           // QString apkLocation = apktextEdit->text();
            QFile *file = new QFile(usersfilename);
            file->open(QIODevice::ReadOnly);
            imagePart.setBodyDevice(file);
            file->setParent(multiPart); 
        
            multiPart->append(textPart);
            multiPart->append(imagePart);
        
            QUrl url("url");
            QNetworkRequest request(url);
        
            QNetworkAccessManager *networkManager= new QNetworkAccessManager;
            QNetworkReply * reply = networkManager->post(request, multiPart);
        

        Can anyone tell me how we select file from directory and upload it to server.

        Thanks in advance.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @developerNancy What error do you get?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        D 1 Reply Last reply
        0
        • jsulmJ jsulm

          @developerNancy What error do you get?

          D Offline
          D Offline
          developerNancy
          wrote on last edited by
          #3

          @jsulm There is no error , nothing display...but I have check on server there is no file.

          jsulmJ 1 Reply Last reply
          0
          • D developerNancy

            @jsulm There is no error , nothing display...but I have check on server there is no file.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @developerNancy In the code you posted there is NO error handling at all!
            You should always check for errors: return values or error signals like http://doc.qt.io/qt-5/qnetworkreply.html#error-1

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            D 1 Reply Last reply
            1
            • jsulmJ jsulm

              @developerNancy In the code you posted there is NO error handling at all!
              You should always check for errors: return values or error signals like http://doc.qt.io/qt-5/qnetworkreply.html#error-1

              D Offline
              D Offline
              developerNancy
              wrote on last edited by developerNancy
              #5

              @jsulm I am using it shows file is uploaded but on server nothing

                 QErrorMessage msg;
                  QString usersfilename = QFileDialog::getOpenFileName();
                  QDir dir = QFileInfo(usersfilename).absoluteDir();
                  qDebug() << dir;
                  qDebug() << usersfilename;
                  this->ui->uploadEdit->setText(usersfilename);
                  QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
                  QHttpPart imagePart;
                  imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain"));
                  imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data;filename=usersfilename"));
                  QHttpPart textPart;
                  textPart.setBody("filename");/*
              
                  QFile *file = new QFile(usersfilename);
                  file->open(QIODevice::ReadOnly);
                  imagePart.setBodyDevice(file);
                  file->setParent(multiPart); /
              
              
                  multiPart->append(textPart);
                  multiPart->append(imagePart);
                  QUrl url("my server url");
                  QNetworkRequest request(url);
                  qDebug() <<url;
              
                  QNetworkAccessManager *networkManager= new QNetworkAccessManager;
                  QNetworkReply * reply = networkManager->post(request, multiPart);
                  multiPart->setParent(reply); // delete the multiPart with the reply
              
                 qDebug() <<reply;
                   if (reply->error() == QNetworkReply::NoError)
                        {
              
                            QString strReply = (QString)reply->readAll();
              
                            //parse json
                            qDebug() << "Response:" << strReply;
                            QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
                            QJsonObject jsonObj = jsonResponse.object();
                            QJsonObject result=jsonObj["result"].toObject();
              
                            if(jsonObj["error"].toBool()==false)
                            {
                                QJsonObject data=result["data"].toObject();
                            
                              QMessageBox msgBox;
                                msgBox.setText("file is uploaded");
                                msgBox.exec();
                            }
                            else
                            {
                                QErrorMessage msg;
                                msg.showMessage("Error during upload proces");
                                msg.exec();
                            }
                            delete reply;
              
                        }
              
              
              
              
              
              }
              
              1 Reply Last reply
              0
              • D Offline
                D Offline
                developerNancy
                wrote on last edited by
                #6

                Done Thanks for the help

                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