Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Upload image file



  • Hi .
    test code is ```
    QUrl testUrl("http://app/test.php");
    QNetworkAccessManager* mgr = new QNetworkAccessManager();
    QNetworkRequest request(testUrl);
    QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);

    QHttpPart textPart;
     textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"text\""));
     textPart.setBody("my text");
    
    QHttpPart imagePart;
    imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
    imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"image\""));
    request.setRawHeader("Content-ID", "my@content.id");
    //request.setRawHeader("User-Agent", "Mozilla/5.21");
    QFile *file = new QFile("C:/xampp/app/images/uploads/product/1024_blue_earth_wallpaper.jpg");
    qDebug() << file->open(QIODevice::ReadOnly);
    imagePart.setBodyDevice(file);
    file->setParent(multiPart);
    multiPart->append(textPart);
    multiPart->append(imagePart);
    
     QNetworkReply *reply = mgr->post(request,multiPart);
    
    php code is 
    

    <?php
    $x="";
    foreach (getallheaders() as $name => $value) {
    $x.="$name: $value\n";
    }

    $json_string = json_encode($_FILES)." boş geçme 111 ".$x.$_POST['text'].$_FILERS['image']['tmp_name'];
    
    $file_handle = fopen('my_filename.json', 'w');
    fwrite($file_handle, $json_string);
    fclose($file_handle);
    //echo "[{}]";
    
    
     //$sonuc = array('sonuc' =>'olumlu' );
     //$sonucValue = array_values($sonuc);
     //echo json_encode($sonucValue);
     $myArr = array( "name"=>"John", "title" => "student");
    

    $myJSON = json_encode($myArr);

    //echo $myJSON;
    //echo "tamamdir";

    //$files = multiple($_FILES);
    //echo json_encode($_FILES);
    //print_r($_FILES);

    ?>

    
    and result 
    
    [] boş geçme 111 Content-ID: my@content.id
    Content-Type: multipart/form-data; boundary="boundary_.oOo._NjAzNg==MTU5NTU=Mjg0NDQ="
    MIME-Version: 1.0
    Content-Length: 288082
    Connection: Keep-Alive
    Accept-Encoding: gzip, deflate
    Accept-Language: en-US,*
    User-Agent: Mozilla/5.0
    Host: app
    my text
    
    my text data  post success  but image data
    how can i solve problem 
    thx for helping


  • Hi SGaist .there is no connection error . everything is ok.

    The following code works

    QUrl testUrl("http://app/test.php");
        QNetworkAccessManager* mgr = new QNetworkAccessManager();
        QNetworkRequest request(testUrl);
        QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    
        QHttpPart textPart;
         textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"text\""));
         textPart.setBody("Merhaba ");
    
         QStringList headers;
         headers
             << "Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
             << "Accept-Encoding	gzip, deflate"
             << "Accept-Language	ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3"
             << "User-Agent	Mozilla/5.0 (X11; Linux x86_64; rv:20.0) Gecko/20100101 Firefox/20.0";
    
        request.setRawHeader("Content-ID", "xxxxxxxxxxxx");
        request.setRawHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        request.setRawHeader("Accept-Encoding",	"gzip, deflate");
        request.setRawHeader("Accept-Language",	"ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3");
        request.setRawHeader("User-Agent",	"Mozilla/5.0 (X11; Linux x86_64; rv:20.0) Gecko/20100101 Firefox/20.0");
        //request.setRawHeader("User-Agent", "Mozilla/5.21");
        QFile *file = new QFile("C:/xampp/app/images/uploads/product/1024_blue_earth_wallpaper.jpg");
        qDebug() << file->open(QIODevice::ReadOnly);
        //qDebug() << file->;
        QFileInfo fileInfo("C:/xampp/app/images/uploads/product/1024_blue_earth_wallpaper.jpg");
        QHttpPart imagePart;
        imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
        imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QString("form-data; name=\"image\";filename=\"%1\"").arg(fileInfo.fileName()).toLatin1()) );
        imagePart.setBodyDevice(file);
        file->setParent(multiPart);
        multiPart->append(textPart);
        multiPart->append(imagePart);
    
         QNetworkReply *reply = mgr->post(request,multiPart);
    


  • hi guys .this code is working on localhost . but not on web http addr
    how can i figure out this problem .

    QUrl testUrl("http://localhost/test.php");
        QNetworkAccessManager* mgr = new QNetworkAccessManager();
        QNetworkRequest request(testUrl);
        QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    
        QHttpPart textPart;
         textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"text\""));
         textPart.setBody("my text");
    
    
        request.setRawHeader("Content-ID", "my@content.id");
        //request.setRawHeader("User-Agent", "Mozilla/5.21");
        QFile *file = new QFile("C:/xampp/app/images/uploads/product/1024_blue_earth_wallpaper.jpg");
        qDebug() << file->open(QIODevice::ReadOnly);
        //qDebug() << file->;
        QFileInfo fileInfo("C:/xampp/app/images/uploads/product/1024_blue_earth_wallpaper.jpg");
        QHttpPart imagePart;
        imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
        imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QString("form-data; name=\"image\";filename=\"%1\"").arg(fileInfo.fileName()).toLatin1()) );
        imagePart.setBodyDevice(file);
        file->setParent(multiPart);
        multiPart->append(textPart);
        multiPart->append(imagePart);
    
         QNetworkReply *reply = mgr->post(request,multiPart);
    
    

  • Lifetime Qt Champion

    Hi,

    You seem to not connect any error related signals. You should add them in order to understand what is happening.



  • Hi SGaist .there is no connection error . everything is ok.

    The following code works

    QUrl testUrl("http://app/test.php");
        QNetworkAccessManager* mgr = new QNetworkAccessManager();
        QNetworkRequest request(testUrl);
        QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    
        QHttpPart textPart;
         textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"text\""));
         textPart.setBody("Merhaba ");
    
         QStringList headers;
         headers
             << "Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
             << "Accept-Encoding	gzip, deflate"
             << "Accept-Language	ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3"
             << "User-Agent	Mozilla/5.0 (X11; Linux x86_64; rv:20.0) Gecko/20100101 Firefox/20.0";
    
        request.setRawHeader("Content-ID", "xxxxxxxxxxxx");
        request.setRawHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        request.setRawHeader("Accept-Encoding",	"gzip, deflate");
        request.setRawHeader("Accept-Language",	"ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3");
        request.setRawHeader("User-Agent",	"Mozilla/5.0 (X11; Linux x86_64; rv:20.0) Gecko/20100101 Firefox/20.0");
        //request.setRawHeader("User-Agent", "Mozilla/5.21");
        QFile *file = new QFile("C:/xampp/app/images/uploads/product/1024_blue_earth_wallpaper.jpg");
        qDebug() << file->open(QIODevice::ReadOnly);
        //qDebug() << file->;
        QFileInfo fileInfo("C:/xampp/app/images/uploads/product/1024_blue_earth_wallpaper.jpg");
        QHttpPart imagePart;
        imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
        imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QString("form-data; name=\"image\";filename=\"%1\"").arg(fileInfo.fileName()).toLatin1()) );
        imagePart.setBodyDevice(file);
        file->setParent(multiPart);
        multiPart->append(textPart);
        multiPart->append(imagePart);
    
         QNetworkReply *reply = mgr->post(request,multiPart);
    

  • Lifetime Qt Champion

    I didn't wrote about connection error, but about connecting the error signal of QNetworkReply to know when something goes wrong.


Log in to reply