Error uploading file on server



  • 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.


  • Moderators

    @developerNancy What error do you get?



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


  • Moderators

    @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



  • @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;
    
              }
    
    
    
    
    
    }


  • Done Thanks for the help


Log in to reply
 

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