Hey folks - thank you so much for your help. Most of this is pretty new to me, and I have been able to only get so far. Could someone familiar with QNetworkRequest and php please help me see where I am going astray?
Patching together several other attempts by various people I came up with this code to upload a file via POST to a php upload script. My code:
... from on_button_clicked() ...
bound="---------------------------7d935033608e2";
crlf=0x0d;
crlf+=0x0a;
data="--"+bound+crlf+"Content-Disposition: form-data; name=\"uploadedFile\";";
data+="filename=\"Contact List.csv\"";
data+=crlf+"Content-Type: text/plain"+crlf+crlf;
data+=inputFile.readAll(); // insert content of text file
data+=crlf+"--"+bound+"--"+crlf;
dataToSend.insert(0,data); // correct/needed?
// request init
QNetworkRequest request
(QUrl("http://edm.localhost/uploader.php"));
// need to set headers here?
reply=manager.post(request,dataToSend); // perform post request
// connections
connect(reply,SIGNAL(uploadProgress(qint64,qint64)),
SLOT(mySetValue(qint64,qint64)));
connect(reply,SIGNAL(finished()),SLOT(replyFinished())); // reply finished - close file
QString bound,data,crlf;
QByteArray dataToSend;
bound="---------------------------7d935033608e2";
crlf=0x0d;
crlf+=0x0a;
data="--"+bound+crlf+"Content-Disposition: form-data; name=\"uploadedFile\";";
data+="filename=\"Contact List.csv\"";
data+=crlf+"Content-Type: text/plain"+crlf+crlf;
data+=inputFile.readAll(); // insert content of text file
data+=crlf+"--"+bound+"--"+crlf;
dataToSend.insert(0,data); // correct/needed?
// request init
QNetworkRequest request(QUrl("http://edm.localhost/uploader.php"));
// need to set headers here?
reply=manager.post(request,dataToSend); // perform post request
// connections
connect(reply,SIGNAL(uploadProgress(qint64,qint64)),
SLOT(mySetValue(qint64,qint64)));
connect(reply,SIGNAL(finished()),SLOT(replyFinished())); // reply finished - close file
To copy to clipboard, switch view to plain text mode
and my php script is:
<?php
$target_path = "";
if($_FILES['uploadedFile']['name'])
$target_path= $target_path.basename($_FILES['uploadedFile']['name']);
else
echo "no file name";
echo "<br>";
if(move_uploaded_file($_FILES['uploadedFile']['tmp_name'], $target_path))
echo "The file ".basename( $_FILES['uploadedFile']['name'])." has been uploaded";
else
echo "There was an error uploading the file, please try again!";
?>
<?php
$target_path = "";
if($_FILES['uploadedFile']['name'])
$target_path= $target_path.basename($_FILES['uploadedFile']['name']);
else
echo "no file name";
echo "<br>";
if(move_uploaded_file($_FILES['uploadedFile']['tmp_name'], $target_path))
echo "The file ".basename( $_FILES['uploadedFile']['name'])." has been uploaded";
else
echo "There was an error uploading the file, please try again!";
?>
To copy to clipboard, switch view to plain text mode
The program seems to function, i.e. no errors or unexpected quitting and the progress bar updates through 100%, but I never receive the uploaded file in my upload directory (or elsewhere) as I do when I access this script through the following HTML form.
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000000" />
Choose a file to upload: <input name="uploadedFile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000000" />
Choose a file to upload: <input name="uploadedFile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
To copy to clipboard, switch view to plain text mode
I'm running a QT4.5 (coding in QTCreator 4.5.2) on an UBUNTU 8.10 LAMP server. Thank you so much for any help!
Bookmarks