PDA

View Full Version : upload an executable file using QNetworkAccessManager



milli
31st May 2011, 00:27
I want to upload an executable file to the server.My code is this:


QString data;
QByteArray dataToSend; // byte array to be sent in POST
QFile *inputFile=new QFile("putty.exe");

if(!inputFile->open(QIODevice::ReadOnly))
return;

data+="\nContent-Disposition: form-data; ";

data+=QString("name=\"%1\"; ").arg(name);//putty
data+=QString("filename=\"%1\";").arg(filepath);//http://localhost/uploadFiles/sdda/putty.exe
data+="Content-Type: application/octet-stream" +inputFile->readAll();

dataToSend=data.toUtf8(); // convert data string to byte array for request

// request init
QNetworkRequest request(QUrl("http://localhost/uploadFile.php"));
request.setRawHeader("Content-Type"," multipart/form-data; boundary=\"-----------------------------7d935033608e2\"");
request.setHeader(QNetworkRequest::ContentLengthHe ader,dataToSend.size());



Running this code the result is:a warning that informs me that a folder in which i want to upload the file already exists.Is the above approach correct?

//php script


<
?php


$packageName = trim($_POST['packageName']);
mkdir("uploadFiles/$packageName");
// Upload file
move_uploaded_file ($_FILES['uploadFile'] ['tmp_name'], "uploads/$packageName/{$_FILES['uploadFile'] ['name']}")

?>

Thanks!

stampede
31st May 2011, 07:54
Where does this warning comes from ? Server or QNAM ?
If it's from server, try to debug on server side (I don't know how, because I don't know anything about php)
I don't really know how to help you with your main issue, but you have possibility of another one - a memory leak here:


if(!inputFile->open(QIODevice::ReadOnly))
return;

You do not release the inputFile before exiting the method, should be more like:


if(!inputFile->open(QIODevice::ReadOnly)){
delete inputFile;
return;
}

wysota
31st May 2011, 08:37
Your uploading code is surely incorrect. Converting a binary blob to a utf-8 encoded string (taking Unicode as the source encoding) is definitely an incorrect approach.

milli
2nd June 2011, 00:33
Problem solved!!For someone else who has similar problem.

This code works..


QString bound;
QString crlf;
QString data;
QByteArray postData;

crlf = 0x0d;
crlf.append(0x0a);
data = "--" + bound + crlf + "Content-Disposition: form-data; name=\"uploadedfile\"; ";
data.append(QString("filename=\"%1\";").arg(filePath));
data.append(crlf + "Content-Type: application/octet-stream" + crlf + crlf);

postData.insert(0,data);
postData.append(file.readAll());
postData.append(crlf + "--" + bound + "--" + crlf);

QUrl url("http://localhost/uploadedFile.php");
QNetworkRequest req(url);
req.setHeader(QNetworkRequest::ContentTypeHeader, tr("multipart/form-data; boundary="));
file.close();
manager->post(req,postData);
connect(manager,SIGNAL(finished(QNetworkReply*)),t his, SLOT(replyFinished(QNetworkReply*)));


And if wou want to delete file from the server then the code is more than a simple:


data.append(QString("packageName=%1").arg(ui->softwareNameEdit->text()));
postData.append(data);
QUrl url("http://localhost/deleteUploadFile.php/");
QNetworkRequest req(url);
manager->post(req,postData);


However,thank's for your answers!!!

milli
3rd June 2011, 21:29
As I mentioned,the process of uploading a file has done using the above code.I want also to make a folder in which i store this file.
For example,if the name of the file
putty.exe then the name of the file would be
applications.
I can make the folder using php scripting.My question is how i can give the name of the folder using qt code?
I can upload the file into the server or give the name of the folder (and the result is the creation of the folder) using post method , but not both of them.
Any help would be appreciated..!

wysota
3rd June 2011, 22:27
I suggest you learn how HTTP works with regard to passing data between the client and the server. I'm sure you have seen webpages with forms where more than one piece of data was posted from the form to the server. You're even looking at one of those pages right now.

milli
4th June 2011, 01:07
I have made what i wanted...!!!!!!Thanks wysota!!!!!!!!!!!!!!