PDA

View Full Version : How to upload file to HTTP server (POST Method)



Alex Snet
31st January 2009, 22:48
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!

jacek
31st January 2009, 22:54
Use QHttp::post().

Alex Snet
31st January 2009, 23:07
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?

jacek
1st February 2009, 02:12
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.

Alex Snet
1st February 2009, 12:40
Like this?



void Screenshot::uploadFrame()
{
QHttpRequestHeader header("GET","/test.php");
header.setValue("Accept","*/*");
header.setValue("Content-Type", "multipart/form-data; boundary=--wp-shotcam");
header.setValue("Host", siteURLEdit->text());
header.setValue("Pragma","no-cache");
//header.setValue("Content-Type","application/x-www-form-urlencoded");
header.setValue("User-Agent","Qlickr: QT Flickr Uploader");
http->setHost(siteURLEdit->text());

//http->request(header);

QByteArray ba;
QBuffer buffer( ba );
buffer.open( IO_WriteOnly );
originalPixmap.save( &buffer , "PNG" );

//originalPixmap
}


I'm on the right way?

jacek
1st February 2009, 15:22
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.

Alex Snet
1st February 2009, 17:10
Thank U!
Problem solved.



void wpShotCam::uploadFrame()
{
http->setHost(siteURLEdit->text());

QByteArray ba;
QBuffer buffer( &ba );
buffer.open( QBuffer::ReadWrite );
originalPixmap.save( &buffer , "PNG" );

http->post("/wp-content/plugins/wp-shotcam.php",ba);
}


And on the server side:


<?php

$f = fopen('./txt/'.time().'.png',"w+");
$body = @file_get_contents('php://input');
fwrite($f,$body);

?>


As sample.

abeinoe
2nd February 2009, 06:01
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{}
?>

bereket
24th January 2011, 22:49
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

QByteArray data;
QBuffer in(&data);
qDebug()<<data.size(); // checking weather ByteArray size expected 0 --- Ok
in.open(QIODevice::ReadWrite);
if(!image.save(&in,"JPEG"))
{
QMessageBox::information(this,"file info", "Image can not be Lodded");
return;
}
qDebug()<<data.size(); // checking weather ByteArray contains the data -- Ok


// uploading the data using http psost

http= new QHttp(this); // initializing http
QHttpRequestHeader header("POST",QUrl::toPercentEncoding("/fileuploder/upload_file.php"));
header.setContentType("multipart/form-data");
header.setValue("Host","http://localhost");
http->setHost("http://localhost");
http->request(header,data);