Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Upload picture with Qt
Forum Updated to NodeBB v4.3 + New Features

Upload picture with Qt

Scheduled Pinned Locked Moved Mobile and Embedded
2 Posts 2 Posters 5.7k 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.
  • A Offline
    A Offline
    anzarQt
    wrote on last edited by
    #1

    hello,
    I want to upload a picture with Qt and send it to a script php that exist in server
    i try this example but it doesn-t work:
    @
    void upload::uploadImage()
    {

    //path image
    QString path("C:/QtProjets/uploadImage/test.png");
    //php script that receives the image
    QNetworkRequest requete(QUrl("http://link server/upload.php")); //our server with php-script
    
    QByteArray boundary = "-------------------------87142694621188";
    QFile file(path);
    if (!file.open(QIODevice::ReadOnly))
    {
        qDebug()<<"erreur read image";
        return;
    }
    QByteArray fileContent(file.readAll());
    
    QByteArray data = "--" + boundary + "\r\n";
    data += "Content-Disposition: form-data; name=\"data\"; filename=\"test.png\";\r\n";
    //add picture to data
    data += "Content-Type: image/png\r\n\r\n" + fileContent + "\r\n";
    data += "--" + boundary + "--\r\n";
    
    requete.setRawHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
    requete.setRawHeader("Content-Length", QString::number(data.size()).toAscii());
    file.close();
    qDebug() <<"data"<< data.size();
    QNetworkAccessManager *am = new QNetworkAccessManager(this);
    QNetworkReply *reply = am->post(requete,"data="+data);
    
    QObject::connect(am, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
    

    }
    /************************************ response /
    /
    ***********************************************************************/

    void upload::replyFinished(QNetworkReply *reply)
    {
    reply->open(QIODevice::ReadOnly);

    // if the response is correct
    if(reply-&gt;error() == QNetworkReply::NoError)
    {
        QByteArray str=(reply-&gt;readAll());
        QString response = QString::fromUtf8(str.data(), str.size());
        qDebug()&lt;&lt;" re "<<response;
    }
    
    //error sever
    
    else
        qDebug()<<"error response server";
    

    }
    @

    and my code php ( upload.php):
    @<?php

    $data =$_POST['data'];
    $fp = fopen("image.png", 'w+');
    fputs($fp, $data);
    fclose($fp);
    echo 'hello'

    ?>

    @
    i think the problem is in this lines 21 ans 22:
    @
    data += "Content-Type: image/jpeg\r\n\r\n" + fileContent + "\r\n";
    data += "--" + boundary + "--\r\n";@
    Any help and advice would be much appreciated!

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chuck Gao
      wrote on last edited by
      #2

      This line:
      @data += "Content-Disposition: form-data; name="data"; filename="test.png";\r\n";@

      should be:

      @data += "Content-Disposition: form-data; name="pic"; filename="test.png";\r\n";@

      Chuck

      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