upload an executable file using QNetworkAccessManager
I want to upload an executable file to the server.My code is this:
Code:
QByteArray dataToSend;
// byte array to be sent in POST
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::ContentLengthHeader,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
Code:
<
?php
$packageName = trim($_POST['packageName']);
mkdir("uploadFiles/$packageName");
// Upload file
move_uploaded_file ($_FILES['uploadFile'] ['tmp_name'], "uploads/$packageName/{$_FILES['uploadFile'] ['name']}")
?>
Thanks!
Re: upload an executable file using QNetworkAccessManager
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:
You do not release the inputFile before exiting the method, should be more like:
Code:
delete inputFile;
return;
}
Re: upload an executable file using QNetworkAccessManager
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.
Re: upload an executable file using QNetworkAccessManager
Problem solved!!For someone else who has similar problem.
This code works..
Code:
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*)),this, SLOT(replyFinished(QNetworkReply*)));
And if wou want to delete file from the server then the code is more than a simple:
Code:
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!!!
Re: upload an executable file using QNetworkAccessManager
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 then the name of the file would be .
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..!
Re: upload an executable file using QNetworkAccessManager
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.
Re: upload an executable file using QNetworkAccessManager
I have made what i wanted...!!!!!!Thanks wysota!!!!!!!!!!!!!!