[solved] QNetworkAccessManager uploading files



  • Hello. I have small problem with uploading files to the http-server. Server is php-based. Everything works fine via uploading form on a web-page, but doesn't work via qt.
    What did I miss in the code below?

    @Tester::Tester(QObject *parent) :
    QObject(parent)
    {
    QNetworkAccessManager *am = new QNetworkAccessManager(this);
    QString path("E:\Chrysanthemum.jpg");
    QNetworkRequest request(QUrl("http://localhost/testuploads.php"));

        QString bound="-------marge";
        QByteArray data(QString("--"+bound+"\r\n").toAscii());
        data += "Content-Disposition: form-data; name=\"action\"\r\n\r\n";
        data += "\r\n";
        data += QString("--" + bound + "\r\n").toAscii();
        data += "Content-Disposition: form-data; name=\"Chrysanthemum\"; filename=\"Chrysanthemum.jpg\"\r\n";
        data += "Content-Type: image/jpg\r\n\r\n";
        QPixmap pix(path);
        QByteArray bytes;
        QBuffer buffer(&bytes);
        buffer.open(QIODevice::WriteOnly);
        pix.save(&buffer, "JPG");
        data.append(bytes);
        data += "\r\n";
        data += QString("--" + bound + "\r\n.").toAscii();
        data += "\r\n";
        request.setRawHeader(QString("Content-Type").toAscii(),QString("multipart/form-data; boundary=" + bound).toAscii());
        request.setRawHeader(QString("Content-Length").toAscii(), QString::number(data.length()).toAscii());
        this->reply = am->post(request,data);
        connect(this->reply, SIGNAL(finished()), this, SLOT(replyFinished()));
    

    }

    void Tester::replyFinished()
    {
    if(this->reply->error() == QNetworkReply::NoError)
    {qDebug() << "TEST";
    QByteArray arr = this->reply->readAll();
    qDebug() << arr.data();
    this->reply->deleteLater();}
    else
    { qDebug() << this->reply->errorString();}
    }@



  • Problem solved!!! Next time I need to read one more time rfc before starting any threads.

    I'll publish the code below and put some comments in it for people who tried to upload files on their own servers:

    @QNetworkAccessManager *am = new QNetworkAccessManager(this);
    QString path("E:\QT_playing\TestingHttpRequests\debug\Chrysanthemum.jpg");
    QNetworkRequest request(QUrl("http://192.168.0.194/testuploads.php")); //our server with php-script

        QString bound="margin"; //name of the boundary
    

    //according to rfc 1867 we need to put this string here:
    QByteArray data(QString("--" + bound + "\r\n").toAscii());
    data.append("Content-Disposition: form-data; name="action"\r\n\r\n");
    data.append("testuploads.php\r\n"); //our script's name, as I understood. Please, correct me if I'm wrong
    data.append("--" + bound + "\r\n"); //according to rfc 1867
    data.append("Content-Disposition: form-data; name="uploaded"; filename="Chrysanthemum.jpg"\r\n"); //name of the input is "uploaded" in my form, next one is a file name.
    data.append("Content-Type: image/jpeg\r\n\r\n"); //data type
    QFile file(path);
    if (!file.open(QIODevice::ReadOnly))
    return;
    data.append(file.readAll()); //let's read the file
    data.append("\r\n");
    data.append("--" + bound + "--\r\n"); //closing boundary according to rfc 1867
    request.setRawHeader(QString("Content-Type").toAscii(),QString("multipart/form-data; boundary=" + bound).toAscii());
    request.setRawHeader(QString("Content-Length").toAscii(), QString::number(data.length()).toAscii());
    this->reply = am->post(request,data);
    qDebug() << data.data();
    connect(this->reply, SIGNAL(finished()), this, SLOT(replyFinished()));@


  • Moderators

    Thanks for sharing your solution.

    I edited the title of your first post and added [solved] to it. That's the way we use in this forum to let people know which topics are solved and have a solution. If you could do that next time w'd be very happy, thanks.



  • can you give me the php server side code?



  • Hi, selvaraman. Here is the code of the testuploads.php:
    @
    <?php
    $target = "Maps/";
    $target = $target . basename( $_FILES['uploaded']['name']) ;
    $ok=1;

    //This is our size condition
    if ($uploaded_size > 10000000)
    {
    echo "Your file is too large.<br>";
    $ok=0;
    }

    //This is our limit file type condition
    if ($uploaded_type =="text/php")
    {
    echo "No PHP files<br>";
    $ok=0;
    }

    //Here we check that $ok was not set to 0 by an error
    if ($ok==0)
    {
    echo "Sorry your file was not uploaded";
    }

    //If everything is ok we try to upload it
    else
    {
    if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
    {
    echo "The file successfully ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
    }
    else
    {
    echo "Sorry, there was a problem uploading your file.";
    }
    }
    ?>

    @

    Here is the code of the uplform.html for testing the previous one (so you have to check the php code, before you'll send request to the server, just in case. If the php script doesn't work, then problem is somewhere else.):
    @<form enctype="multipart/form-data" action="testuploads.php" method="POST">
    Please choose a file: <input name="uploaded" type="file" /><br />
    <input type="submit" value="Upload" />
    </form> @

    I used this code in my school project and it worked perfectly both at school and at home.



  • dmitrij, Thanks for sharing your code ..



  • Heyy @dmitrij Can You please send the whole source code as zip. I just want to refer it. Thank You


Log in to reply
 

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