简体   繁体   中英

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

I sent an excel file in a post request to my back-end and I tried handling the file using the following:

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

However, the output from that is not an excel file. It is a collection of XML files. My end goal is to obtain a data frame from the file data that was sent so that I can extract the data.

What is a clean way of handling this type of file?

file_data = request.FILES[KEY].file

Returns the file uploaded in the form of bytes. The following will read the excel file as a data frame:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM