Here is a complete code about this thread. There is no authentication progress with this code.
void MainWindow::sendfile()
{
QFile file("licensefile");
//lets get the file by filename {
qDebug() << "file open failure"; //send message if file cant open
}
QByteArray line;
//a qbytearray object for read file line by line while (!file.atEnd())
{
line.append(file.readLine());
}
//we read file line by line with no error handling for reading time!!
file.close();
QByteArray boundary;
//actually i cant understand that why we are using a second byte array for file sending. // if someone know this trick please write below. I write this code like the other examples.
datas += "Content-Disposition: form-data; name=\"file\"; filename=\""+file.fileName()+"\"\r\n";
//here is the http header for manuplate a normal http form and form file object
datas += "Content-Type: image/jpeg\r\n\r\n"; //file type is here
datas += line; //and our file is giving to form object
datas += "\r\n";
datas
+= QString("--" + boundary
+ "\r\n\r\n").
toAscii();
datas += "Content-Disposition: form-data; name=\"upload\"\r\n\r\n";
datas += "Uploader\r\n";
datas
+= QString("--" + boundary
+ "--\r\n").
toAscii();
QNetworkRequest req;
req.
setUrl(QUrl("http://192.168.0.167/input.php"));
//my virtual servers' ip address and tiny php page url is here req.setRawHeader("Content-Type", "multipart/form-data; boundary=" + boundary); // we must set the first header like this. its tell the server, current object is a form
QNetworkAccessManager *manager = new QNetworkAccessManager; //using qnetwork access manager for post data
connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(erroron_filesend(QNetworkReply*))); //connecting manager object for errors here
manager->post(req,datas); //send all data
}
void MainWindow::erroron_filesend(QNetworkReply *replye)
{
if (replye->error() !=0)
{
QMessageBox::information(this,
"Connection Error",replye
->errorString
());
return;
}
else
{
QMessageBox::information(this,
"Upload Complete", message_d
);
return;
}
}
void MainWindow::sendfile()
{
QFile file("licensefile"); //lets get the file by filename
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) //accessibility controll for file
{
qDebug() << "file open failure"; //send message if file cant open
}
QByteArray line; //a qbytearray object for read file line by line
while (!file.atEnd())
{
line.append(file.readLine());
}
//we read file line by line with no error handling for reading time!!
file.close();
QByteArray boundary; //actually i cant understand that why we are using a second byte array for file sending.
// if someone know this trick please write below. I write this code like the other examples.
QByteArray datas(QString("--" + boundary + "\r\n").toAscii());
datas += "Content-Disposition: form-data; name=\"file\"; filename=\""+file.fileName()+"\"\r\n";
//here is the http header for manuplate a normal http form and form file object
datas += "Content-Type: image/jpeg\r\n\r\n"; //file type is here
datas += line; //and our file is giving to form object
datas += "\r\n";
datas += QString("--" + boundary + "\r\n\r\n").toAscii();
datas += "Content-Disposition: form-data; name=\"upload\"\r\n\r\n";
datas += "Uploader\r\n";
datas += QString("--" + boundary + "--\r\n").toAscii();
QNetworkRequest req;
req.setUrl(QUrl("http://192.168.0.167/input.php")); //my virtual servers' ip address and tiny php page url is here
req.setRawHeader("Content-Type", "multipart/form-data; boundary=" + boundary); // we must set the first header like this. its tell the server, current object is a form
QNetworkAccessManager *manager = new QNetworkAccessManager; //using qnetwork access manager for post data
connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(erroron_filesend(QNetworkReply*))); //connecting manager object for errors here
manager->post(req,datas); //send all data
}
void MainWindow::erroron_filesend(QNetworkReply *replye)
{
if (replye->error() !=0)
{
QMessageBox::information(this,"Connection Error",replye->errorString());
return;
}
else
{
QString message_d = QString("Upload Complete");
QMessageBox::information(this, "Upload Complete", message_d);
return;
}
}
To copy to clipboard, switch view to plain text mode
Php Code
<?php
$filename = $_FILES['file']['name'];
$source = $_FILES['file']['tmp_name'];
$target = 'upload/'.$filename;
move_uploaded_file($source, $target)
?>
<?php
$filename = $_FILES['file']['name'];
$source = $_FILES['file']['tmp_name'];
$target = 'upload/'.$filename;
move_uploaded_file($source, $target)
?>
To copy to clipboard, switch view to plain text mode
Its a very simple code and dont try to upload big files (up to 1 Mbyte) with this code.
This one is very dirty i know. Its will be helpfull, i wish... Sorry for bad English. Regards.
Bookmarks