如何处理在 Django 中 POST 请求中发送的 excel 文件?

How to handle excel file sent in POST request in Django?

我在 post 请求中向我的后端发送了一个 excel 文件,我尝试使用以下方法处理该文件:

def __handle_file(file):
destination = open('./data.xslx', 'wb')
for chunk in file.chunks():
    destination.write(chunk)
destination.close()

但是,它的输出不是 excel 文件。它是 XML 个文件的集合。 我的最终目标是从发送的文件数据中获取数据框,以便我可以提取数据。

处理此类文件的简洁方法是什么?

file_data = request.FILES[KEY].file

Returns以字节形式上传的文件。 以下将读取 excel 文件作为数据框:

data = pd.read_excel(io.BytesIO(file_data.read()))