Results 1 to 9 of 9

Thread: How to upload file to HTTP server (POST Method)

  1. #1
    Join Date
    Feb 2008
    Location
    Russia, Moscow
    Posts
    35
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question How to upload file to HTTP server (POST Method)

    Hi all!

    How I can upload file to an HTTP Server (php script) with POST Method?

    I need to upload an image that I get making screenshot of my screen.

    Please, help!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to upload file to HTTP server (POST Method)


  3. #3
    Join Date
    Feb 2008
    Location
    Russia, Moscow
    Posts
    35
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to upload file to HTTP server (POST Method)

    Thanks, but I use it.

    Can U give me a small sample how I can upload a file?
    And what I need to do for upload an image from QPixmap?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to upload file to HTTP server (POST Method)

    Quote Originally Posted by Alex Snet View Post
    And what I need to do for upload an image from QPixmap?
    You have to convert the pixmap into binary form. For example by saving it into a buffer. The rest depends on what data that PHP script expects. Either set the content type to image/something and send the image directly or use multipart/form-data content type.

  5. #5
    Join Date
    Feb 2008
    Location
    Russia, Moscow
    Posts
    35
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to upload file to HTTP server (POST Method)

    Like this?

    Qt Code:
    1. void Screenshot::uploadFrame()
    2. {
    3. QHttpRequestHeader header("GET","/test.php");
    4. header.setValue("Accept","*/*");
    5. header.setValue("Content-Type", "multipart/form-data; boundary=--wp-shotcam");
    6. header.setValue("Host", siteURLEdit->text());
    7. header.setValue("Pragma","no-cache");
    8. //header.setValue("Content-Type","application/x-www-form-urlencoded");
    9. header.setValue("User-Agent","Qlickr: QT Flickr Uploader");
    10. http->setHost(siteURLEdit->text());
    11.  
    12. //http->request(header);
    13.  
    14. QBuffer buffer( ba );
    15. buffer.open( IO_WriteOnly );
    16. originalPixmap.save( &buffer , "PNG" );
    17.  
    18. //originalPixmap
    19. }
    To copy to clipboard, switch view to plain text mode 

    I'm on the right way?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to upload file to HTTP server (POST Method)

    Yes, you're going in the right direction. Change "GET" to "POST" in line #3. You'll also have to decorate the image data with proper headers (content-disposition, content-type etc.) and those --wp-shotcam markers.

  7. #7
    Join Date
    Feb 2008
    Location
    Russia, Moscow
    Posts
    35
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to upload file to HTTP server (POST Method)

    Thank U!
    Problem solved.

    Qt Code:
    1. void wpShotCam::uploadFrame()
    2. {
    3. http->setHost(siteURLEdit->text());
    4.  
    5. QBuffer buffer( &ba );
    6. buffer.open( QBuffer::ReadWrite );
    7. originalPixmap.save( &buffer , "PNG" );
    8.  
    9. http->post("/wp-content/plugins/wp-shotcam.php",ba);
    10. }
    To copy to clipboard, switch view to plain text mode 

    And on the server side:
    Qt Code:
    1. <?php
    2.  
    3. $f = fopen('./txt/'.time().'.png',"w+");
    4. $body = @file_get_contents('php://input');
    5. fwrite($f,$body);
    6.  
    7. ?>
    To copy to clipboard, switch view to plain text mode 

    As sample.

  8. #8
    Join Date
    Jan 2009
    Posts
    11

    Default Re: How to upload file to HTTP server (POST Method)

    this is my code to send txt file using POST method. the main point is how to construct data as message http body to send via POST. i cancel the file handle to show you all data in my text file.

    void UploadDialog::sendToServer(bool checked)
    {
    if(ui->path->text()!=""){
    //QFile fileHand(ui->path->text());
    //if(!fileHand.open(QIODevice::ReadOnly | QIODevice::Text)){
    // ui->debugBox->insertPlainText(tr("File can not be opened"));
    //}

    QString bound;
    QString data;
    QString crlf;
    QByteArray dataToSend;
    bound = "---------------------------7d935033608e2";
    crlf = 0x0d;
    crlf += 0x0a;
    data = "--" + bound + crlf + "Content-Disposition: form-data; name=\"fupload\"; ";
    data += "filename=\"C:\\coba upload.txt\"";
    data += crlf + "Content-Type: text/plain" + crlf + crlf;//if you want to send binary file use "Content-Type: Application/Octet"
    data += "isi dari file"; //this is my text file content
    data+= crlf + "--" + bound + "--" + crlf;
    dataToSend.insert(0,data);

    QHttpRequestHeader header("POST","/lpse/upload_hand.php");
    header.setContentType(tr("multipart/form-data; boundary=") + bound);
    header.addValue("Host","lktixampp");
    header.addValue("Connection","Keep-Alive");

    ui->debugBox->insertPlainText(header.toString()+"\n");
    ui->debugBox->insertPlainText(dataToSend.data());

    http->setHost("lktixampp",QHttp::ConnectionModeHttp);
    httpId = http->request(header,dataToSend);

    //fileHand.close();
    }
    }
    lktixampp is my web server using xampp and the php code is:
    <?php
    $file_location = $_FILES['fupload']['tmp_name'];
    $file_name = $_FILES['fupload']['name'];
    $dir_file = "files/$file_name";
    if(move_uploaded_file($file_location,"$dir_file")) {}
    else{}
    ?>

  9. #9
    Join Date
    Jan 2011
    Posts
    1
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to upload file to HTTP server (POST Method)

    Hello guys
    I am trying to upload picture using Qhttp post unfortunately I am not successful
    it seems serialization is ok, but I suspect header has problem
    plz plz help
    Qt Code:
    1. QBuffer in(&data);
    2. qDebug()<<data.size(); // checking weather ByteArray size expected 0 --- Ok
    3. in.open(QIODevice::ReadWrite);
    4. if(!image.save(&in,"JPEG"))
    5. {
    6. QMessageBox::information(this,"file info", "Image can not be Lodded");
    7. return;
    8. }
    9. qDebug()<<data.size(); // checking weather ByteArray contains the data -- Ok
    10.  
    11.  
    12. // uploading the data using http psost
    13.  
    14. http= new QHttp(this); // initializing http
    15. QHttpRequestHeader header("POST",QUrl::toPercentEncoding("/fileuploder/upload_file.php"));
    16. header.setContentType("multipart/form-data");
    17. header.setValue("Host","http://localhost");
    18. http->setHost("http://localhost");
    19. http->request(header,data);
    To copy to clipboard, switch view to plain text mode 

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.