Forums

OverviewV-Play 2 Support (Qt 5) › upload jpeg to server created from camera

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #14888

    Bas

    hi,

    i try to upload a picture taken with Camera to a server and handle it in PHP.

    1. i take the picture
    2. the image data is converted to a base64 encoded string by calling a C++ function exposed to QML
      QString ImageUrl2Base64::getImageDataByUrl(QString url)
      {
          QFile fileImg(url);
      
          fileImg.open(QIODevice::ReadOnly);
          QByteArray imageData = fileImg.readAll();
          QByteArray imageData_Base64 = imageData.toBase64();
      
          QString imgdata(imageData_Base64);
      
          return imgdata;
      }
      
      

       

    3. the encoded data is posted using XMLHttpRequest
      function post2Server(fname,img)
              {
                  var url = "http://server";
                  var params = "filename=" + fname + "&imgdata=" + img;
                  var xhr = new XMLHttpRequest();
                  xhr.open('POST', url, false);
      
                  //-- headers
                  xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                  xhr.setRequestHeader("Content-length", params.length);
                  xhr.setRequestHeader("Connection", "close");
      
                  xhr.onreadystatechange = (function(myxhr) {
                      return function() {
                          if(myxhr.readyState === XMLHttpRequest.HEADERS_RECEIVED)
                          {
              //                console.log(myxhr.responseText)
                          }
                          else if( myxhr.readyState === XMLHttpRequest.DONE)
                          {
                              if (myxhr.status === 200)
                              {
                                  console.log(myxhr.responseText)
      //                            var data =  JSON.parse(myxhr.responseText);
                                  if(data !== null ) {
                                      //cb(data);
                                  }
                              }
                              else
                              {
                                  console.log(myxhr.responseText)
                              }
                          }
                      }
                  })(xhr);
                  xhr.send(params);
              }
      
      

       

    4. on server i decode it using a php script
      <?php
      $upload_dir = "../upload/";
      $fname = $_POST['filename'];
      $img = $_POST['imgdata'];
      
      $img=substr($img, strpos($img, ",")+1);
      $img = str_replace(' ', '+', $img);
      
      $data = base64_decode($img);
      
      $file = $upload_dir . "" .basename($fname);
      $success = file_put_contents($file, $data);
      print $success ? $file : 'Unable to save the file.';
      ?>

       

    the image data on server is corrupted, and when i compare the image data from taken picture and server image are different.

     

    is this the right way to do it?

    i could not find anything simpler.

    thanx!

    #14890

    Bas

    ok i got it now,

    it had todo with the base64 encoding of QT and base64 decoding of PHP

    PHP does not like spaces in the string and needs to be converted to ‘+’ to get proper base64 decoding.

    $img = str_replace(‘ ‘,’+’,$img);

    <?php
    $upload_dir = "../upload/";
    
    $fname = $_POST['filename'];
    $img = $_POST['imgdata'];
    
    //-- convert ' ' space to '+' when getting base64 encoded string from QT
    $img = str_replace(' ','+',$img);
    $data = base64_decode($img);
    
    $file = $upload_dir . "" .basename($fname);
    $success = file_put_contents($file, $data);
    print $success ? $file : 'Unable to save the file.';
    ?>

     

Viewing 2 posts - 1 through 2 (of 2 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
FREE!
create apps
create games
cross platform
native performance
3rd party services
game network
multiplayer
level editor
easiest to learn
biggest time saving
best support