Results 1 to 7 of 7

Thread: QHttpMultiPart - sending image to web serwer question

  1. #1
    Join Date
    Oct 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default QHttpMultiPart - sending image to web serwer question

    Hi,

    i'am trying to post image to web server using QHttpMultiPart, and ewerything was fine until i moved my php part to new web server... now images i'am saving have only 4bytes and when i try to open them in browser it says that the file is corrupted. What can be the couse of such behaving ?

    on request finished replay var_dump($_POST) gives me this:

    Qt Code:
    1. array(3) {
    2. ["idx"]=>
    3. string(2) "10"
    4. ["qname"]=>
    5. string(9) "test1.jpg"
    6. ["qfile"]=>
    7. string(4) "˙Ř˙ŕ" << here is the problem, length is only 4
    8. }
    To copy to clipboard, switch view to plain text mode 

    but when i post to old server i get

    Qt Code:
    1. array(3) {
    2. ["idx"]=>
    3. string(2) "10"
    4. ["qname"]=>
    5. string(9) "test1.jpg"
    6. ["qfile"]=>
    7. string(112871) "˙Ř˙ŕ << here length is ok
    To copy to clipboard, switch view to plain text mode 

    in wireshark post looks fine for me. Code i'am using is taken form QHttpMultiPart class reference, so it's pretty basic.
    thanks for any help and sorry for my poor english.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QHttpMultiPart - sending image to web serwer question

    Nothing to do with Qt.

    I will take a guess: the fifth byte in the original file is a zero byte and this is being treated as a string terminator. Does the binary part show up in the PHP _FILES global?
    Qt Code:
    1. <?php
    2.  
    3. if(!$_FILES) {
    4. echo "No file!"
    5. }
    6.  
    7. $id = $_FILES['image']['name'];
    8. move_uploaded_file($_FILES['image']['tmp_name'], "/permanent/location".$id);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QHttpMultiPart - sending image to web serwer question

    hmm i think i checked $_FILES and it was NULL, but i'am gonna check again tomorrow.

    and it's normal that in about 10 files i checked fifth byte is 0 ?

    hmmm

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QHttpMultiPart - sending image to web serwer question

    You said the bytes are being sent correctly so the problem is in how PHP interprets them.

    I guessed a zero byte because because that's a common side effect of treating binary as text. Your example is a JPEG mage. JPEG images start with the bytes 0xFF 0xD8 0xFF 0xE0 followed by two byte block length (big-end first) and the four letters JFIF etc. In the case the header block is small (usually) the fifth byte of the file will be 0. The characters you see are what I would expect if PHP were treating the bytes as characters encoded according to the ISO/IEC 8859-2:1999 (Latin-2) or Windows CP1250 (Eastern Europe) 8-bit encoding.

  5. #5
    Join Date
    Oct 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QHttpMultiPart - sending image to web serwer question

    hm i get it i think but still, don't know how to fix it :P
    php file that receives post is in utf-8, qt sends file probably in cp1250 as i'am polish.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QHttpMultiPart - sending image to web serwer question

    You should use the _FILES super global to access binary uploaded files from a temporary directory on the server
    OR
    encode the binary data as Base64 (or some other text only encoding) for use in the _POST variable.

    See http://php.net/manual/en/features.file-upload.php

    To have the file appear in _FILES modify the doc example:
    Qt Code:
    1. QHttpPart imagePart;
    2. imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
    3. imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"image\"; filename=\"image.jpg\"")); // <<< filename triggers separate handling
    4. QFile *file = new QFile("image.jpg");
    5. file->open(QIODevice::ReadOnly);
    6. imagePart.setBodyDevice(file);
    7. file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
    To copy to clipboard, switch view to plain text mode 
    and then you will see in the PHP environment:
    Qt Code:
    1. chrisw@newton /tmp/simple_example $ ./simple_example
    2. // _POST array
    3. array(1) {
    4. ["text"]=>
    5. string(7) "my text"
    6. }
    7. // _FILES array
    8. array(1) {
    9. ["image"]=>
    10. array(5) {
    11. ["name"]=>
    12. string(9) "image.jpg"
    13. ["type"]=>
    14. string(10) "image/jpeg"
    15. ["tmp_name"]=>
    16. string(14) "/tmp/phpaLYWWk"
    17. ["error"]=>
    18. int(0)
    19. ["size"]=>
    20. int(16246)
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Oct 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QHttpMultiPart - sending image to web serwer question

    ok, thanks for help, i'am going to try it tomorrow at work and see how it works

Similar Threads

  1. sending soap request using QNetworkAccessManager question
    By babymonsta in forum Qt Programming
    Replies: 10
    Last Post: 21st April 2010, 07:55
  2. Replies: 1
    Last Post: 23rd October 2009, 18:23
  3. sending image one class to another class
    By rajini333 in forum Qt Programming
    Replies: 4
    Last Post: 4th June 2009, 09:45
  4. Sending a image using QSocket & QServerSocket
    By machathu in forum Qt Programming
    Replies: 1
    Last Post: 28th March 2006, 12:23
  5. Question about updating an image on screen
    By SkripT in forum Qt Programming
    Replies: 1
    Last Post: 24th February 2006, 19:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.