Results 1 to 6 of 6

Thread: Upload file to HTTP server

  1. #1
    Join Date
    Feb 2010
    Location
    Poland
    Posts
    27
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Upload file to HTTP server

    Hi.
    I'm trying to write class for uploading files to HTTP server. I have a one, which uses QNetworkAccessManager::post(const QNetworkRequest & request, const QByteArray & data) method, but all the time when i want to upload file - i have to load entire(!) of file content into memory (QByteArray). I decided to find a better way to do the same. I stumbled on this project:
    http://code.google.com/p/datacod-qt-tools/

    and in uploader.cpp file
    (http://code.google.com/p/datacod-qt-...r/uploader.cpp, from line no. 85)

    i found that i can do it better by creating class that inherits QIODevice and reimplementing virtual qint64 readData(char *data, qint64 maxlen) and virtual qint64 size() methods. When you want to upload file - just create instance of this class and pass it to QNetworkAccessManager::post(const QNetworkRequest & request, QIODevice * data) method. Here's prepared class:
    http://code.google.com/p/datacod-qt-...er/qupfile.cpp

    It seems to be a good solution and i thought that it'll work. Unfortunately - it doesn't. QUpFile::readData(char *data, qint64 maxlen) is not called even once and uploadProgress(qint64 bytesWritten, qint64 bytesTotal) signal is emitted only one time (bytesWritten = bytesTotal = 0).

    Anybody have idea what's wrong with it or have another way to upload file without loading entire of its content into memory?

    Thank you in advance.
    Greet.

  2. #2
    Join Date
    Feb 2010
    Location
    Poland
    Posts
    27
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Upload file to HTTP server

    Solved. If somebody needs help with similar problem - just write here :)

  3. #3
    Join Date
    Jan 2011
    Location
    Gordion
    Posts
    52
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Upload file to HTTP server

    I need a simple solution about this... I Keeping search too.
    Regards.
    Kos Asker.

  4. #4
    Join Date
    Jan 2011
    Location
    Gordion
    Posts
    52
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Upload file to HTTP server

    Here is a complete code about this thread. There is no authentication progress with this code.


    Qt Code:
    1. void MainWindow::sendfile()
    2. {
    3.  
    4. QFile file("licensefile"); //lets get the file by filename
    5. if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) //accessibility controll for file
    6. {
    7. qDebug() << "file open failure"; //send message if file cant open
    8. }
    9. QByteArray line; //a qbytearray object for read file line by line
    10. while (!file.atEnd())
    11. {
    12. line.append(file.readLine());
    13. }
    14. //we read file line by line with no error handling for reading time!!
    15.  
    16. file.close();
    17. QByteArray boundary; //actually i cant understand that why we are using a second byte array for file sending.
    18. // if someone know this trick please write below. I write this code like the other examples.
    19.  
    20. QByteArray datas(QString("--" + boundary + "\r\n").toAscii());
    21. datas += "Content-Disposition: form-data; name=\"file\"; filename=\""+file.fileName()+"\"\r\n";
    22. //here is the http header for manuplate a normal http form and form file object
    23.  
    24. datas += "Content-Type: image/jpeg\r\n\r\n"; //file type is here
    25. datas += line; //and our file is giving to form object
    26. datas += "\r\n";
    27. datas += QString("--" + boundary + "\r\n\r\n").toAscii();
    28. datas += "Content-Disposition: form-data; name=\"upload\"\r\n\r\n";
    29. datas += "Uploader\r\n";
    30. datas += QString("--" + boundary + "--\r\n").toAscii();
    31.  
    32. QNetworkRequest req;
    33. req.setUrl(QUrl("http://192.168.0.167/input.php")); //my virtual servers' ip address and tiny php page url is here
    34. req.setRawHeader("Content-Type", "multipart/form-data; boundary=" + boundary); // we must set the first header like this. its tell the server, current object is a form
    35.  
    36. QNetworkAccessManager *manager = new QNetworkAccessManager; //using qnetwork access manager for post data
    37.  
    38. connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(erroron_filesend(QNetworkReply*))); //connecting manager object for errors here
    39. manager->post(req,datas); //send all data
    40.  
    41. }
    42.  
    43. void MainWindow::erroron_filesend(QNetworkReply *replye)
    44. {
    45. if (replye->error() !=0)
    46. {
    47. QMessageBox::information(this,"Connection Error",replye->errorString());
    48. return;
    49. }
    50. else
    51. {
    52. QString message_d = QString("Upload Complete");
    53. QMessageBox::information(this, "Upload Complete", message_d);
    54. return;
    55. }
    56. }
    To copy to clipboard, switch view to plain text mode 

    Php Code
    Qt Code:
    1. <?php
    2. $filename = $_FILES['file']['name'];
    3. $source = $_FILES['file']['tmp_name'];
    4. $target = 'upload/'.$filename;
    5. move_uploaded_file($source, $target)
    6. ?>
    To copy to clipboard, switch view to plain text mode 

    Its a very simple code and dont try to upload big files (up to 1 Mbyte) with this code.
    This one is very dirty i know. Its will be helpfull, i wish... Sorry for bad English. Regards.
    Last edited by kosasker; 27th May 2011 at 15:26.

  5. #5
    Join Date
    Jul 2011
    Posts
    1
    Qt products
    Qt4

    Default Re: Upload file to HTTP server

    Hi produktdotestow,

    I'm quite interested in your project http://code.google.com/p/datacod-qt-tools/ because I'm currently developing an upload manager with Qt and using .NET web service at the server side. I read your code but I realized that you use a form at the server side. So in my case, how can pass parameters (username and password) and QUpFile to web service.

    thank you very much!
    Trung

  6. #6
    Join Date
    Feb 2010
    Location
    Poland
    Posts
    27
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Upload file to HTTP server

    This
    http://code.google.com/p/datacod-qt-tools/
    is not my project, but it's not important.

    As i understood you want to upload file and pass username and password to server, yes?

    1. Get working QUpFile class
    2. Create instance
    3. Set head and tail using setHead and setTail methods
    Qt Code:
    1. upfile->setHead("------------hKdQAaQIL4zeExpbXAmwlW\r\nContent-Disposition: form-data; name=\"username\"\r\n\r\nusername\r\n------------hKdQAaQIL4zeExpbXAmwlW\r\nContent-Disposition: form-data; name=\"password\"\r\n\r\npassword\r\n------------hKdQAaQIL4zeExpbXAmwlW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"yourFileName\"\r\nContent-Type: application/octet\r\n\r\n");
    2. upfile->setTail("\r\n------------hKdQAaQIL4zeExpbXAmwlW--");
    To copy to clipboard, switch view to plain text mode 
    4. Create QNetworkRequest object and set Content-Type header like that:
    Qt Code:
    1. request.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data; boundary=----------hKdQAaQIL4zeExpbXAmwlW");
    To copy to clipboard, switch view to plain text mode 
    5. Use
    Qt Code:
    1. QNetworkReply* QNetworkAccessManager::post(const QNetworkRequest &request, QIODevice *data)
    To copy to clipboard, switch view to plain text mode 
    to post your request to server:
    Qt Code:
    1. QNetworkReply *reply = networkAccessManager->post(request, upfile);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 0
    Last Post: 28th February 2011, 09:10
  2. errors while upload files on http server????
    By sups in forum Qt Programming
    Replies: 4
    Last Post: 25th February 2011, 23:11
  3. How to upload file to HTTP server (POST Method)
    By Alex Snet in forum Qt Programming
    Replies: 8
    Last Post: 24th January 2011, 22:49
  4. QFtp:upload file to server
    By ensky_cy in forum Qt Programming
    Replies: 6
    Last Post: 14th December 2009, 10:42
  5. server upload
    By ag.sitesh in forum Qt Programming
    Replies: 1
    Last Post: 1st April 2008, 13:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.