1

Warning: This is not purely a code question, is about knowing "why" something actually works.

I am trying to use Flask to parse an XML file the user imports using a form without needing to save it anywhere on the server. After reading this thread:

Read file data without saving it in Flask

I've understood that Flask passes the file as FileStorage object, a streaming object in memory which has a .read() method that can be used to open the file. My trouble is that the file is not utc-8 encoded and if I try to read it offline I would get an encoding error. However, when I do it with this FileStorage object, no encoding needs to be provided and the code works just fine as below.

@app.route('/API/parsefile/', methods=['POST'])
def getfile():
    if request.method == 'POST':

        # import the file from the webform
        file = request.files['xbrfinput']

        # Read the file
        pf = bs(io.BytesIO(file.read()), "lxml")

        return pf.find("xxx")

Would somebody care to explain to me why this works? Does the FileStorage object guess the encoding? Thanks

comendeiro
  • 816
  • 7
  • 14

0 Answers0