[SOLVED] Using QHttpMultipart for a HTML form



  • Greetings!

    I got a problem with QNetworkAccessmanager::post() method in uploading a file programmatically using QHttpMultipart.

    I got success using an HTML form via QWebView. The HTML form looks like this:

    @<form method="post" action="[url value received in response]" enctype="multipart/form-data">
    <!-- MUST FIELDS -->
    <input type="hidden" name="MAX_FILE_SIZE" value="[max_file_size value received in response]">
    <input type="hidden" name="UPLOAD_IDENTIFIER" value="[upload_identifier value received in response]">
    <input type="hidden" name="extra_info" value="[extra_info value received in response]">
    <input type="file" name="userfile">
    <input type="submit" value="Submit">
    </form>@

    It doesn't help a lot since I need to select a file (without user interaction) and submit it to a certain url. I followed the guide in the QHttpmultipart documentation but "fails" every-time.



  • I already solved it. Seems like there is something wrong with the documentation for QHttpMultiPart. The documentation points out RFC 2046 but can't find the answer as I search for it.

    I was able to google RFC 1867 which solved the problem.

    In the documentation:
    @QHttpPart imagePart;
    imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
    imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name="image""));@

    I think the problem was that ContentDispositionHeader should come first before ContentTypeHeader.
    I tried this using the "file" input control and not with an "image" as cited in the code;



  • Seems like my method is too buggy. Sometimes it uploads a file and sometimes it doesn't.... I don't know why.

    Well, I found a solution from "TuckDesgin":http://www.tuckdesign.com/sources/Qt and modified it.

    ... and it worked for me everytime, I just need to test it again and again just to make sure.



  • Thank you Code_ReaQtor for sharing the link to TuckDesign's solution. It really does the trick! :)



  • [quote author="dv879" date="1361446791"]Thank you Code_ReaQtor for sharing the link to TuckDesign's solution. It really does the trick! :)[/quote]

    Sure. You're welcome.


Log in to reply
 

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