我正面临上传文件的另一个问题
我无法将文件上传到php脚本,也无法读取输出
我认为uploader.upload()很好,问题只是标题(Web服务器是Apache)
我的Uploader.upload()让我发疯:(
知道代码有什么问题吗?
from PyQt4 import QtCore, QtGui, QtNetwork
class Window(QtGui.QWidget):
def __init__(self, address):
QtGui.QWidget.__init__(self)
self.address = address
self.table = QtGui.QTableWidget(self)
header = self.table.horizontalHeader()
header.setStretchLastSection(True)
header.hide()
self.table.setColumnCount(2)
self.button = QtGui.QPushButton('Add Upload', self)
self.button.clicked.connect(self.handleAddUpload)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.table)
layout.addWidget(self.button)
self.netaccess = QtNetwork.QNetworkAccessManager(self)
self._uploaders = {}
def handleAddUpload(self):
stream = QtCore.QFile('/home/me/icon.png')
if stream.open(QtCore.QIODevice.ReadOnly):
#data = stream.readAll()
#stream.close()
data = stream
row = self.table.rowCount()
button = QtGui.QPushButton('Abort', self.table)
button.clicked.connect(lambda: self.handleAbort(row))
progress = QtGui.QProgressBar(self.table)
progress.setRange(0, data.size())
self.table.setRowCount(row + 1)
self.table.setCellWidget(row, 0, button)
self.table.setCellWidget(row, 1, progress)
uploader = self._uploaders[row] = Uploader(row, self.netaccess)
uploader.uploadProgress.connect(self.handleUploadProgress)
uploader.uploadFinished.connect(self.handleUploadFinished)
uploader.upload(data, self.address)
def handleUploadProgress(self, key, sent, total):
print('upload(%d): %d [%d]' % (key, sent, total))
progress = self.table.cellWidget(key, 1)
progress.setValue(sent)
def handleUploadFinished(self, key):
print('upload(%d) finished' % key)
button = self.table.cellWidget(key, 0)
button.setDisabled(True)
uploader = self._uploaders.pop(key)
uploader.deleteLater()
def handleAbort(self, key):
try:
self._uploaders[key].abort()
except (KeyError, AttributeError):
pass
class Uploader(QtCore.QObject):
uploadProgress = QtCore.pyqtSignal(object, int, int)
uploadFinished = QtCore.pyqtSignal(object)
def __init__(self, key, parent):
QtCore.QObject.__init__(self, parent)
self._key = key
self._reply = None
def abort(self):
if self._reply is not None:
self._reply.abort()
def upload(self, data, url):
if self._reply is None:
self._stream = data
self._multiPart = QtNetwork.QHttpMultiPart(QtNetwork.QHttpMultiPart.FormDataType)
imagePart = QtNetwork.QHttpPart()
fileName = QtCore.QFileInfo(self._stream.fileName()).fileName()
key = 'image_field'
imagePart.setHeader(QtNetwork.QNetworkRequest.ContentDispositionHeader,
"form-data; name=\"%s\"; filename=\"%s\"" % (key, fileName))
imagePart.setHeader(QtNetwork.QNetworkRequest.ContentTypeHeader,
'image/png')
imagePart.setHeader(QtNetwork.QNetworkRequest.ContentLengthHeader,
self._stream.size())
imagePart.setBodyDevice(self._stream);
self._multiPart.append(imagePart)
request = QtNetwork.QNetworkRequest(QtCore.QUrl(url))
request.setHeader(QtNetwork.QNetworkRequest.ContentTypeHeader,
'multipart/form-data; boundary=%s' % self._multiPart.boundary())
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):
self.uploadProgress.emit(self._key, sent, total)
if sent >= total:
# prevent duplicated uploads
self._reply.close()
def handleFinished(self):
#print(self._reply.readAll()) #uncomment it if you want see output
self._multiPart.deleteLater()
self._stream.deleteLater() #self._strem.close()
self._reply.deleteLater()
self._reply = None
self.uploadFinished.emit(self._key)
def handleError(self):
print(self._reply.error())
print(self._reply.errorString())
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window('http://localhost/01/upload/123.php')
window.setGeometry(500, 300, 500, 300)
window.show()
sys.exit(app.exec_())
这是我的HTML表单:
<form enctype="multipart/form-data" method="post" action="123.php">
<input type="file" size="32" name="image_field" value="">
<input type="submit" name="Submit" value="upload">
</form>