Hi everyone, I have a problem about how to upload a file QHttp, I can download a file from a webserver:
it works fine, and runs well on the simulator, but I can not upload file to the serverCode:getFile(const QUrl &url, const QString &filePath) { QHttp _http.setHost(url.host(), url.port(80)); _http.get(url.path(), &_file); _http.close(); }
PHP code as follows:Code:QString fileName = "C:\\test.txt"; QString openFilesPath = fileName; QFile *userfile = new QFile(openFilesPath); if(!userfile->open(QIODevice::ReadOnly)) return; QByteArray byt(openFilesPath.toUtf8()); QByteArray bytes; bytes.append("--AaB03x\r\n"); bytes.append("content-disposition: "); bytes.append("form-data; type=\"file\" ;name=\"contact\"\r\n"); bytes.append("\r\n"); bytes.append("0\r\n"); bytes.append("--AaB03x\r\n"); bytes.append("content-disposition: "); bytes.append("form-data; type=\"file\" ; name=\"contact\"; filename=\"" + byt+ "\"\r\n"); bytes.append("Content-Transfer-Encoding: binary\r\n"); bytes.append("\r\n"); bytes.append(userfile->readAll()); userfile->close(); bytes.append("\r\n"); bytes.append("--AaB03x--"); int contentLength = bytes.length(); _http.setHost("host"); QHttpRequestHeader header("POST", "/a/b.php/c/doUpload") ; header.setValue("Host", "host") ; header.setContentType("application/x-www-form-urlencoded"); _http.request(header, bytes) ;
Code:http://host/a/b.php/c/doUpload <?php class testAction extends Action { function doUpload(){ $d ="D:\\test\\".$_FILES['contact']['name']; if(is_uploaded_file($_FILES['contact']['tmp_name'])){ move_uploaded_file($_FILES['contact']['tmp_name'], $d); } function upload(){ $this->display(); } } } ?>

Reply With Quote

