def sendUpload(row): upload = UploadList.uploads[row] upload['uploading'] = True filename = upload['name'].split('/')[-1] multiPart = QtNetwork.QHttpMultiPart( QtNetwork.QHttpMultiPart.ContentType.RelatedType) filedata = QtNetwork.QHttpPart() filedata.setHeader(QtNetwork.QNetworkRequest.ContentTypeHeader, "image/jpeg") filedata.setHeader(QtNetwork.QNetworkRequest.ContentDispositionHeader, "form-data; name=media;") file = QtCore.QFile(upload['name']) file.open(QtCore.QIODevice.ReadOnly) file.setParent(multiPart) upload['stream'] = file filedata.setBodyDevice(file) multiPart.append(filedata) upload['multiPart'] = multiPart url = QtCore.QUrl('http://127.0.0.1:8000/medias') request = QtNetwork.QNetworkRequest() request.setUrl(url) request.setRawHeader('Authorization'.encode(), ('Bearer ' + Login.token).encode()) request.setRawHeader('FILENAME'.encode(), filename.encode()) upload['manager'] = QtNetwork.QNetworkAccessManager() try: upload['reply'] = upload['manager'].post(request, upload['multiPart']) upload['manager'].setParent(upload['reply']) upload['reply'].uploadProgress.connect(updateProgress(upload, row)) except Exception as e: print(e)
def construct_multipart(files): multiPart = QtNetwork.QHttpMultiPart(QtNetwork.QHttpMultiPart.FormDataType) for key, file in files.items(): imagePart = QtNetwork.QHttpPart() fileName = QtCore.QFileInfo(file.fileName()).fileName() imagePart.setHeader( QtNetwork.QNetworkRequest.ContentDispositionHeader, "form-data; name=\"%s\"; filename=\"%s\"" % (key, fileName)) imagePart.setBodyDevice(file) multiPart.append(imagePart) return multiPart