QHttpMultipart To fill a Form



  • Hi,
    I would like to fill a form with qt but it seems something goes wrong because my program crash ;

    The Form type in HTML :

    <form name="frmIndex" method="post" enctype="multipart/form-data" action="SendSMS.php">
    				
    	<p>Destination : <input id="file" name="FichierDest" type="file"></p>
    	<p>Username : <input name="username" pb-role="username" type="text"></p>
    	<p>Password : <input name="password" pb-role="password" type="password"></p>
    	<p><textarea maxlength="300" cols="50" rows="5" name="message"></textarea></p>
    	<p><input value="Valider" pb-role="submit" type="submit"></p>
    </form>
    

    My code in Qt :

    
        /*  -------------------------------------
        QLineEdit *username;
        QLineEdit *password;
        QLineEdit *text;
          --------------------------------------- */
        QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    
        QHttpPart usernamePart;
        usernamePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"username\""));
        const char *Username = username->text().toStdString().c_str();
        usernamePart.setBody(QByteArray(Username));
    
        QHttpPart passwordPart;
        passwordPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"password\""));
        const char *Password = password->text().toStdString().c_str();
        passwordPart.setBody(QByteArray(Password));
    
        QHttpPart textPart;
        textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"message\""));
        const char *Text = text->text().toStdString().c_str();
        textPart.setBody(QByteArray(Text));
    
        QHttpPart destinationFilePart;
        destinationFilePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain"));
        destinationFilePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"FichierDest\""));
        QFile *file = new QFile(nomFichier->text());
        file->open(QIODevice::ReadOnly);
        destinationFilePart.setBodyDevice(file);
        file->setParent(multiPart);
    
        multiPart->append(destinationFilePart);
        multiPart->append(usernamePart);
        multiPart->append(passwordPart);
        multiPart->append(textPart);
    
    
        const QUrl url = QUrl("http://111.111.111.111/web2sms/SendSMS.php");
        const QNetworkRequest request(url);
    
        QNetworkAccessManager *manager;
        QNetworkReply *reply = manager->post(request, multiPart);
        multiPart->setParent(reply);
    
        // ---------------------------------------------------
    
        connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
                this,SLOT(messageErreur(QNetworkReply::NetworkError)));
    
        connect(reply,SIGNAL(finished()),
                this, SLOT(fin()));
    

    To sum up : "I just want to upload a file, send name, password and text with a same request in post method"

    May you Help me ?


  • Moderators

    @Network1618033 said in QHttpMultipart To fill a Form:

    because my program crash

    If your program crashes then simply use debugger and execute it step by step to see where exactly it crashes. Most probably one of the many pointers you're using isn't pointing to a valid object.


  • Qt Champions 2017

    @Network1618033 said in QHttpMultipart To fill a Form:

    QNetworkAccessManager *manager;

    you dont new it as in

    QNetworkAccessManager *manager = QNetworkAccessManager ;

    and you try to call a function via invalid object
    manager->post(xxx) <<< BOOM



  • Thank you all it works now !


Log in to reply
 

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