×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    27

    How to upload file with QHttp?

    Hi everyone, I have a problem about how to upload a file QHttp, I can download a file from a webserver:
    Code:
    getFile(const QUrl &url, const QString &filePath)
    {
            QHttp _http.setHost(url.host(), url.port(80));	
    	_http.get(url.path(), &_file);	
    	_http.close();
    }
    it works fine, and runs well on the simulator, but I can not upload file to the server
    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) ;
    PHP code as follows:
    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();
          }       
        }    
    }
    ?>

  2. #2
    Registered User
    Join Date
    Jul 2009
    Posts
    27

    Re: How to upload file with QHttp?

    after catching the http request message, and making some changes it is done.

  3. #3
    Regular Contributor
    Join Date
    Jul 2007
    Location
    Dublin
    Posts
    240

    Re: How to upload file with QHttp?

    QHttp is deprecated class. You should be using classes QNetworkAccessManager, QNetworkRequest, QNetworkReply to build your code.
    Ref: http://wiki.forum.nokia.com/index.ph..._QNetworkReply

Similar Threads

  1. file upload
    By bejoy_ak in forum Mobile Java Networking & Messaging & Security
    Replies: 9
    Last Post: 2010-04-14, 04:01
  2. Replies: 19
    Last Post: 2010-03-29, 08:09
  3. QNetworkAccessManager or QHttp
    By gino.rossi in forum Qt
    Replies: 6
    Last Post: 2010-02-24, 09:12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×