Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt WebKit
  4. [Solved]QNetworkAccessManager Upload Image to WebService
Forum Updated to NodeBB v4.3 + New Features

[Solved]QNetworkAccessManager Upload Image to WebService

Scheduled Pinned Locked Moved Qt WebKit
5 Posts 3 Posters 4.4k 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.
  • F Offline
    F Offline
    flaviomarcio
    wrote on last edited by
    #1

    Hello, I try to upload an image with QNetworkAccessManager to webServiceREST, and I have no success.

    I know he works with Delphi. Tested and confirmed.

    With Qt I load all the bytes to the server, but the error occurs at the end.

    what I did wrong in Qt?

    Application message:

    starting D:\projetos\Zdemos\qt\rest\Demo1\build-restDemo1-Desktop_Qt_5_2_1_MinGW_32bit-Debug\debug\restDemo1.exe...
    bytesSent == 16384 == 85848 bytesTotal
    bytesSent == 85848 == 85848 bytesTotal
    299 == slotError
    bytesSent bytesTotal == 0 == 0
    statusCodeV. == "500"

    web service error:
    {"error": "TsmServerMethodsUnit.updateUploadImagem method not found in the list server method"}

    Application terminate.

    Notes:
    1-Metode updateUploadImagem not exist in the webservice, is the correct metode UploadImagem I not then inform metode updateUploadImagem name, Qt did THAT?

    2)The parameter in webservice is a Stream.

    3)In example in delphi, I make execute perfect.

    code:

    @
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    ui->lineEdit->setText("http://www.mechame.homeip.net:8080/datasnap/rest/DSAdmin/GetPlatformName");
    
    this->manager = new QNetworkAccessManager(this);
    QObject::connect(this->manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedSlot(QNetworkReply*)));
    

    }
    @

    @

    void MainWindow::on_pushButton_3_clicked()
    {

    QString filePath("C:/TEMP/0.jpg");
    QUrl url("http://www.mechame.homeip.net:8080/datasnap/rest/TsmServerMethodsUnit/UploadImagem");
    
    
    QFile *file = new QFile(filePath);
    file->open(QIODevice::ReadOnly);
    
    QNetworkRequest request;
    request.setUrl(url);
    //request.setHeader(QNetworkRequest::ContentTypeHeader,"application/octet-stream"); //Required!
    request.setHeader(QNetworkRequest::ContentTypeHeader,"text/plain"); //Required!
    
    QNetworkReply *reply = this->manager->post(request,file->readAll());
    file->setParent(reply);
    
    QObject::connect(reply, SIGNAL(uploadProgress(qint64,qint64)), this, SLOT(onUploadProgress(qint64,qint64)));
    QObject::connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError(QNetworkReply::NetworkError)));
    

    }

    @

    @void MainWindow::finishedSlot(QNetworkReply* reply)
    {

    QMessageBox msgBox;
    
    QVariant statusCodeV =reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    // "200 OK" received?
    
    qDebug()<<"statusCodeV.=="<<statusCodeV.toString();
    
    QByteArray bytes = reply->readAll(); // bytes
    const QString string(bytes); // string
    
    ui->textBrowser->setText(string);
    
    
    if (statusCodeV.toInt()==200)
    {
        msgBox.setText("Successfull!...");
    }
    else
    {
        msgBox.setText("UnSuccessfull!...");
    }
    msgBox.exec&#40;&#41;;
    

    }

    void MainWindow::slotReadyRead()
    {
    //Q_UNUSED(request);
    }

    void MainWindow::slotSslErrors(QList<QSslError> erros)
    {
    qDebug()<<"slotSslErrors=="<<erros;
    }

    void MainWindow::slotError(QNetworkReply::NetworkError error)
    {
    qDebug()<<"slotError=="<<error;
    }

    @

    Flavio Portela

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi,

      Please edit your post and add '@' before and after your code. It's very hard to read.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • F Offline
        F Offline
        flaviomarcio
        wrote on last edited by
        #3

        Sorry, I organize better.

        Flavio Portela

        1 Reply Last reply
        0
        • T Offline
          T Offline
          ThatDude
          wrote on last edited by
          #4
          1. It looks like "data snap requires the stream to be in JSON format":http://stackoverflow.com/questions/18110654/upload-file-to-datasnap-rest-server-via-tstream

          2. It is required "to quote Method name":http://docwiki.embarcadero.com/RADStudio/XE5/en/DataSnap_REST_Messaging_Protocol - at least while I was testing with your server it didn't accept unquoted names/properties

          Then all you have to do is to create/print the binary data/bytes to a JSON string e.g.: [37,80,68,70,45,49,46,51,13,10]

          @
          QUrl url("http://www.mechame.homeip.net:8080/datasnap/rest/TsmServerMethodsUnit/%​22UploadImagem%​22/");
          // ...read a file
          QByteArray baFile = file->readAll();
          QString json("[");
          json.reserve(baFile.size()*4);
          for (int i=0; i<baFile.size(); ++i)
          json.append(QString("%1,").arg((unsigned char)baFile[i]));
          if (json.size() > 1)
          json.resize(json.size()-1);
          json.append("]");
          QByteArray ba (json.toStdString().c_str());

          QNetworkReply *reply = this->manager->post(request, ba);
          

          @

          @
          finishedSlot()
          {"result":["4D006A61EA3F37DD54E0A3C3DD85ADE9"]}
          Successfull!...@

          NB! If you copy and paste - remove the zero width space I had to insert between % and 22 otherwise it wouldn't print properly.

          1 Reply Last reply
          0
          • F Offline
            F Offline
            flaviomarcio
            wrote on last edited by
            #5

            Wow, okay, thank you.

            Flavio Portela

            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