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
Forum Updated to NodeBB v4.3 + New Features

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 2 May 2017, 04:53 last edited by developerNancy 5 Feb 2017, 04:54
    #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.

    J 1 Reply Last reply 2 May 2017, 05:21
    0
    • D Offline
      D Offline
      developerNancy
      wrote on 2 May 2017, 11:30 last edited by
      #6

      Done Thanks for the help

      1 Reply Last reply
      0
      • D developerNancy
        2 May 2017, 04:53

        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.

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 2 May 2017, 05:21 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 2 May 2017, 10:16
        0
        • J jsulm
          2 May 2017, 05:21

          @developerNancy What error do you get?

          D Offline
          D Offline
          developerNancy
          wrote on 2 May 2017, 10:16 last edited by
          #3

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

          J 1 Reply Last reply 2 May 2017, 10:44
          0
          • D developerNancy
            2 May 2017, 10:16

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

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 2 May 2017, 10:44 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 2 May 2017, 10:48
            1
            • J jsulm
              2 May 2017, 10:44

              @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 2 May 2017, 10:48 last edited by developerNancy 5 Feb 2017, 10:49
              #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 2 May 2017, 11:30 last edited by
                #6

                Done Thanks for the help

                1 Reply Last reply
                0

                2/6

                2 May 2017, 05:21

                topic:navigator.unread, 4
                • Login

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