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!
Printable View
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!
Use QHttp::post().
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?
Like this?
Code:
void Screenshot::uploadFrame() { 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; buffer.open( IO_WriteOnly ); originalPixmap.save( &buffer , "PNG" ); //originalPixmap }
I'm on the right way?
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.
Thank U!
Problem solved.
Code:
void wpShotCam::uploadFrame() { http->setHost(siteURLEdit->text()); QByteArray ba; originalPixmap.save( &buffer , "PNG" ); http->post("/wp-content/plugins/wp-shotcam.php",ba); }
And on the server side:
Code:
<?php $f = fopen('./txt/'.time().'.png',"w+"); $body = @file_get_contents('php://input'); fwrite($f,$body); ?>
As sample.
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.
lktixampp is my web server using xampp and the php code is:Quote:
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();
}
}
Quote:
<?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{}
?>
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
Code:
QByteArray data; qDebug()<<data.size(); // checking weather ByteArray size expected 0 --- Ok if(!image.save(&in,"JPEG")) { return; } qDebug()<<data.size(); // checking weather ByteArray contains the data -- Ok // uploading the data using http psost header.setContentType("multipart/form-data"); header.setValue("Host","http://localhost"); http->setHost("http://localhost"); http->request(header,data);