from PyQt4 import QtCore, QtGui, QtNetwork
import sys
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.netaccess = QtNetwork.QNetworkAccessManager(self)
self._uploaders = {}
row = 1 #this number will change per upload in real program,this is just demo
self.address = 'http://dev/demo.php'
stream
= QtCore.
QFile('icon.png') data = stream
uploader = self._uploaders[row] = Uploader(row, self.netaccess)
uploader.upload(data, self.address)
def __init__(self, key, parent):
QtCore.
QObject.__init__
(self, parent
) self._key = key
self._reply = None
def upload(self, data, url):
if self._reply is None:
self._stream = data
self._multiPart = QtNetwork.QHttpMultiPart(QtNetwork.QHttpMultiPart.FormDataType)
fileName
= QtCore.
QFileInfo(self._stream.
fileName()).
fileName() key = 'file'
imagePart = QtNetwork.QHttpPart()
imagePart.setHeader(QtNetwork.QNetworkRequest.ContentDispositionHeader,
"form-data; name=\"%s\"; filename=\"%s\"" % (key, fileName))
imagePart.setHeader(QtNetwork.QNetworkRequest.ContentTypeHeader,
'image/png')
imagePart.setBodyDevice(self._stream)
submitPart = QtNetwork.QHttpPart()
submitPart.setHeader(QtNetwork.QNetworkRequest.ContentDispositionHeader,
"form-data; name=\"submit\"")
submitPart.setBody("submit")
self._multiPart.append(imagePart)
self._multiPart.append(submitPart)
request
= QtNetwork.
QNetworkRequest(QtCore.
QUrl(url
)) request.setHeader(QtNetwork.QNetworkRequest.ContentTypeHeader,
'multipart/form-data; boundary=%s' % self._multiPart.boundary())
request.setRawHeader('User-Agent','Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36')
self._reply = self.parent().post(request, self._multiPart)
self._reply.uploadProgress.connect(self.handleUploadProgress)
self._reply.error.connect(self.handleError)
self._reply.finished.connect(self.handleFinished)
def handleUploadProgress(self, sent, total):
print sent, total
def handleFinished(self):
print('Content: ',self._reply.readAll())#no output here :(
self._stream.close()
self._multiPart.deleteLater()
self._reply.deleteLater()
self._reply = None
app.quit()
def handleError(self):
print('Error String :',self._reply.errorString())
print('Error number: ',self._reply.error())
demo = Window()
sys.exit(app.exec_())
from PyQt4 import QtCore, QtGui, QtNetwork
import sys
class Window(QtGui.QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.netaccess = QtNetwork.QNetworkAccessManager(self)
self._uploaders = {}
row = 1 #this number will change per upload in real program,this is just demo
self.address = 'http://dev/demo.php'
stream = QtCore.QFile('icon.png')
if stream.open(QtCore.QIODevice.ReadOnly):
data = stream
uploader = self._uploaders[row] = Uploader(row, self.netaccess)
uploader.upload(data, self.address)
class Uploader(QtCore.QObject):
def __init__(self, key, parent):
QtCore.QObject.__init__(self, parent)
self._key = key
self._reply = None
def upload(self, data, url):
if self._reply is None:
self._stream = data
self._multiPart = QtNetwork.QHttpMultiPart(QtNetwork.QHttpMultiPart.FormDataType)
fileName = QtCore.QFileInfo(self._stream.fileName()).fileName()
key = 'file'
imagePart = QtNetwork.QHttpPart()
imagePart.setHeader(QtNetwork.QNetworkRequest.ContentDispositionHeader,
"form-data; name=\"%s\"; filename=\"%s\"" % (key, fileName))
imagePart.setHeader(QtNetwork.QNetworkRequest.ContentTypeHeader,
'image/png')
imagePart.setBodyDevice(self._stream)
submitPart = QtNetwork.QHttpPart()
submitPart.setHeader(QtNetwork.QNetworkRequest.ContentDispositionHeader,
"form-data; name=\"submit\"")
submitPart.setBody("submit")
self._multiPart.append(imagePart)
self._multiPart.append(submitPart)
request = QtNetwork.QNetworkRequest(QtCore.QUrl(url))
request.setHeader(QtNetwork.QNetworkRequest.ContentTypeHeader,
'multipart/form-data; boundary=%s' % self._multiPart.boundary())
request.setRawHeader('User-Agent','Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36')
self._reply = self.parent().post(request, self._multiPart)
self._reply.uploadProgress.connect(self.handleUploadProgress)
self._reply.error.connect(self.handleError)
self._reply.finished.connect(self.handleFinished)
def handleUploadProgress(self, sent, total):
print sent, total
def handleFinished(self):
print('Content: ',self._reply.readAll())#no output here :(
self._stream.close()
self._multiPart.deleteLater()
self._reply.deleteLater()
self._reply = None
app.quit()
def handleError(self):
print('Error String :',self._reply.errorString())
print('Error number: ',self._reply.error())
app = QtGui.QApplication(sys.argv)
demo = Window()
sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode
Using this simple PHP file upload receiver:
$ cat demo.php
<html>
<body>
<form method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
<p>
<?php
if(isset($_POST['submit'])) {
if ($_FILES["file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"] . "<br />";
} else {
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
}
?>
</p>
</body>
</html>
$ cat demo.php
<html>
<body>
<form method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
<p>
<?php
if(isset($_POST['submit'])) {
if ($_FILES["file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"] . "<br />";
} else {
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
}
?>
</p>
</body>
</html>
To copy to clipboard, switch view to plain text mode
Gives this output with an artificially large icon.png:
16384 530779
32768 530779
...
524288 530779
530779 530779
('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'))
16384 530779
32768 530779
...
524288 530779
530779 530779
('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
Bookmarks