[Solved]QNetworkAccessManager Upload Image to WebService



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

    @


  • Moderators

    Hi,

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



  • Sorry, I organize better.



    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.



  • Wow, okay, thank you.


Log in to reply
 

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