Results 1 to 18 of 18

Thread: Http Posting using QNetworkAccessManager

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #15
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Http Posting using QNetworkAccessManager

    Yes it is. Your code with only the web server and file path changed, the submit button encoded, and the close() call removed:
    Qt Code:
    1. from PyQt4 import QtCore, QtGui, QtNetwork
    2. import sys
    3. class Window(QtGui.QWidget):
    4. def __init__(self, parent=None):
    5. super(Window, self).__init__(parent)
    6.  
    7. self.netaccess = QtNetwork.QNetworkAccessManager(self)
    8. self._uploaders = {}
    9. row = 1 #this number will change per upload in real program,this is just demo
    10. self.address = 'http://dev/demo.php'
    11. stream = QtCore.QFile('icon.png')
    12. if stream.open(QtCore.QIODevice.ReadOnly):
    13. data = stream
    14.  
    15. uploader = self._uploaders[row] = Uploader(row, self.netaccess)
    16.  
    17. uploader.upload(data, self.address)
    18.  
    19. class Uploader(QtCore.QObject):
    20.  
    21. def __init__(self, key, parent):
    22. QtCore.QObject.__init__(self, parent)
    23. self._key = key
    24. self._reply = None
    25.  
    26.  
    27. def upload(self, data, url):
    28. if self._reply is None:
    29.  
    30. self._stream = data
    31.  
    32. self._multiPart = QtNetwork.QHttpMultiPart(QtNetwork.QHttpMultiPart.FormDataType)
    33.  
    34. fileName = QtCore.QFileInfo(self._stream.fileName()).fileName()
    35. key = 'file'
    36.  
    37. imagePart = QtNetwork.QHttpPart()
    38. imagePart.setHeader(QtNetwork.QNetworkRequest.ContentDispositionHeader,
    39. "form-data; name=\"%s\"; filename=\"%s\"" % (key, fileName))
    40. imagePart.setHeader(QtNetwork.QNetworkRequest.ContentTypeHeader,
    41. 'image/png')
    42. imagePart.setBodyDevice(self._stream)
    43.  
    44. submitPart = QtNetwork.QHttpPart()
    45. submitPart.setHeader(QtNetwork.QNetworkRequest.ContentDispositionHeader,
    46. "form-data; name=\"submit\"")
    47. submitPart.setBody("submit")
    48.  
    49. self._multiPart.append(imagePart)
    50. self._multiPart.append(submitPart)
    51.  
    52. request = QtNetwork.QNetworkRequest(QtCore.QUrl(url))
    53. request.setHeader(QtNetwork.QNetworkRequest.ContentTypeHeader,
    54. 'multipart/form-data; boundary=%s' % self._multiPart.boundary())
    55. request.setRawHeader('User-Agent','Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36')
    56.  
    57. self._reply = self.parent().post(request, self._multiPart)
    58. self._reply.uploadProgress.connect(self.handleUploadProgress)
    59. self._reply.error.connect(self.handleError)
    60. self._reply.finished.connect(self.handleFinished)
    61.  
    62.  
    63.  
    64. def handleUploadProgress(self, sent, total):
    65. print sent, total
    66.  
    67. def handleFinished(self):
    68. print('Content: ',self._reply.readAll())#no output here :(
    69. self._stream.close()
    70. self._multiPart.deleteLater()
    71. self._reply.deleteLater()
    72. self._reply = None
    73. app.quit()
    74.  
    75. def handleError(self):
    76. print('Error String :',self._reply.errorString())
    77. print('Error number: ',self._reply.error())
    78.  
    79.  
    80. app = QtGui.QApplication(sys.argv)
    81. demo = Window()
    82. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 
    Using this simple PHP file upload receiver:
    Qt Code:
    1. $ cat demo.php
    2. <html>
    3. <body>
    4. <form method="post" enctype="multipart/form-data">
    5. <label for="file">Filename:</label>
    6. <input type="file" name="file" id="file" />
    7. <br />
    8. <input type="submit" name="submit" value="Submit" />
    9. </form>
    10. <p>
    11. <?php
    12. if(isset($_POST['submit'])) {
    13. if ($_FILES["file"]["error"] > 0) {
    14. echo "Error: " . $_FILES["file"]["error"] . "<br />";
    15. } else {
    16. echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    17. echo "Type: " . $_FILES["file"]["type"] . "<br />";
    18. echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    19. echo "Stored in: " . $_FILES["file"]["tmp_name"];
    20. }
    21. }
    22. ?>
    23. </p>
    24. </body>
    25. </html>
    To copy to clipboard, switch view to plain text mode 
    Gives this output with an artificially large icon.png:
    Qt Code:
    1. 16384 530779
    2. 32768 530779
    3. ...
    4. 524288 530779
    5. 530779 530779
    6. ('Content: ', PyQt4.QtCore.QByteArray('<html>\n <body>\n <form method="post" enctype="multipart/form-data">\n <label for="file">Filename:</label>\n <input type="file" name="file" id="file" /> \n <br />\n <input type="submit" name="submit" value="Submit" />\n </form>\n <p>\n Upload: icon.png<br />Type: image/png<br />Size: 517.9921875 Kb<br />Stored in: /tmp/phpKg8FG7 </p>\n </body>\n</html>\n'))
    To copy to clipboard, switch view to plain text mode 
    Last edited by ChrisW67; 23rd January 2014 at 23:07. Reason: Better PHP

  2. The following user says thank you to ChrisW67 for this useful post:

    Mohammadhzp (24th January 2014)

Similar Threads

  1. Replies: 2
    Last Post: 8th January 2014, 15:40
  2. Replies: 4
    Last Post: 2nd May 2012, 12:51
  3. QNetworkAccessManager and http redirection
    By grayfox in forum Qt Programming
    Replies: 5
    Last Post: 8th July 2011, 17:24
  4. HTTP Post from QNetworkAccessManager - no data sent
    By secureboot in forum Qt Programming
    Replies: 1
    Last Post: 13th April 2011, 18:46
  5. QNetworkAccessManager Http Basic Authentication?
    By jloundy in forum Qt Programming
    Replies: 5
    Last Post: 29th December 2010, 00:19

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.