QNetworkAccessManager: HTTP orqali fayllarni yuborish

Faqat PHP bilan apache serverida bir nechta faylni yuklash uchun QNetworkAccessManagerdan foydalanish kerak.

Bu yerda men Python ad QT (PySide) uchun yangi bo'lganim sababli bu erda so'rayapman va kimdir buni ko'rsatadigan kodni yoki python kodi misolini bilmoqchi edim.

Eslatma: Agar ushbu turdagi kontent mavjud bo'lmasa, men uni o'zim qilishga urinib ko'rdim va keyinchalik darslik bilan o'rtoqlashmoqchiman.

1
Qt 4.8dan boshlab (va 1.0.8 pyside) siz QHttpMultiPart dan foydalanishingiz mumkin.
qo'shib qo'ydi muallif alexisdm, manba
Men QT 4.7 bilan ishlayapman, lekin link :) uchun rahmat!
qo'shib qo'ydi muallif Fy-, manba

1 javoblar

Buning eng yaxshi usuli ekanini bilmayman, lekin u yaxshi ishlaydi.

    class Upload():
        def __init__(self):
            self.url     = QUrl("http://www.website.com/up.php")
            self.manager = QtNetwork.QNetworkAccessManager()

        def sendRequest(self, replayfile):
            self.request = QtNetwork.QNetworkRequest(self.url)              

            self.request.setRawHeader("Host", str(self.url.host()));
            self.request.setRawHeader("Content-type", "multipart/form-data; boundary=AaB03x");

            fp = QFile(replayfile)
            fp.open(QIODevice.ReadOnly)
            bytes = QByteArray()

            bytes.append("--AaB03x\r\n")
            bytes.append("Content-Disposition: ")
            bytes.append("form-data; name=\"replay\"; filename=\"" + QByteArray(str(QFileInfo(replayfile).fileName())) + "\"\r\n")
            bytes.append("Content-Type: %s\r\n"%mimetypes.guess_type(str(replayfile))[0])
            bytes.append("\r\n")
            bytes.append(fp.readAll())

            fp.close()       

            bytes.append("\r\n")
            bytes.append("--AaB03x--")

            contentLength = bytes.length()
            self.request.setRawHeader("Content-Length", "%s" % contentLength)       

            self.manager.post(self.request, bytes)  

PHP-da:

    Array
    (
        [replay] => Array
            (
                [name] => test.SC2Replay
                [type] => None
                [tmp_name] => /tmp/phpK6tChh
                [error] => 0
                [size] => 9882
            )

    )
3
qo'shib qo'ydi
Python
Python
372 ishtirokchilar

Bu guruh python dasturlash tilini muhokama qilish uchun. Iltimos, o'zingizni hurmat qiling va faqat dasturlash bo'yicha yozing. Botlar mavzusini @botlarhaqida guruhida muhokama qling! FAQ: @PyFAQ Offtopic: @python_uz_offtopic

Python offtopic group !
Python offtopic group !
150 ishtirokchilar

@python_uz gruppasining offtop gruppasi. offtop bo'lsa ham reklama mumkin emas ) Boshqa dasturlash tiliga oid gruppalar @languages_programming