RestRequest.AddFileBytes发布损坏的文件

时间:2019-02-27 20:03:30

标签: restsharp

我正在尝试使用RestRequest.AddFileBytes将图像文件发布到服务中,但是当我这样做时,文件似乎已损坏。当我将其下载回去时,文件大小过小,并且取决于文件类型,它的扩展名可能似乎错误或无法解释(可能是b / c标头被弄乱了)

当我尝试对Postman做同样的事情时,文件就可以了,所以似乎与我发布文件的方式有所不同。请指教!

# Form implementation generated from reading ui file 'adcombo.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon, QPixmap

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(6, 10, 791, 361))
        self.label.setFrameShape(QtWidgets.QFrame.WinPanel)
        self.label.setText("")
        self.label.setObjectName("label")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(120, 430, 231, 91))
        self.pushButton.setStyleSheet("color: rgb(255, 0, 0);\n"
"background-color: rgb(79, 79, 79);\n"
"font: 75 16pt \"MS Shell Dlg 2\";")
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "Chart Type"))


class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.create_menu()

    def create_menu(self):
        menu = QtWidgets.QMenu(
            self.pushButton, 
            triggered=self.on_menu_triggered
        )
        for text in ("", "Bar", "Scatter", "Box"):
            menu.addAction(text)
        self.pushButton.setMenu(menu)

    @QtCore.pyqtSlot(QtWidgets.QAction)
    def on_menu_triggered(self, action):
        pixmap = QtGui.QPixmap()
        if not action.text():
            self.label.clear()
            return
        if action.text() == 'Bar':
            pixmap = QPixmap('bar.png')
        elif action.text() == 'Scatter':
            pixmap= QPixmap('scatter.png')
        elif action.text() == 'Box':
            pixmap = QPixmap('box.png')
        self.label.setPixmap(pixmap)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

您不知道吗,就像发条一样,我在2天的大部分时间里都敲了一下脑袋,然后发布了这个问题,几乎立即想出了如何使它起作用。我不会留下我知道为什么一个人是正确的,而一个人只是主要是的权利(这显然不够好),但是

我只需要更改

request.AddFileBytes(fileName, bytes, fileName, contentType);

request.AddParameter(contentType, bytes, ParameterType.RequestBody);