0

I am sending a gzip file from Postman to a Flask endpoint. I can take that binary file with request.data and read it, save it, upload it, etc.

My problem is that I can't take its name. How can I do that?

My gzip file is called "test_file.json.gz" and my file is called "test_file.json".

How can I take any of those names?

Edit:

I'm taking the stream data with io.BytesIO(), but this library doesn't contain a name attribute or something, although I can see the file name into the string if I just:

>>>print(request.data)
>>>b'\x1f\x8b\x08\x08\xca\xb1\xd3]\x00\x03test_file.json\x00\xab\xe6RPP\xcaN\xad4T\xb2RP*K\xcc)M5T\xe2\xaa\x05\x00\xc2\x8b\xb6;\x16\x00\x00\x00'
Claudiu Iova
  • 15
  • 1
  • 9
  • Please post the code which handles the upload. – v25 Nov 19 '19 at 11:52
  • It's nothing to be posted. I need a simplistic way to get that file's name. It's clear that taking the stream with BytesIO doesn't give me the file name as well! – Claudiu Iova Nov 19 '19 at 12:19

1 Answers1

1

Further to the comment, I think the code which handles your upload is relevant here.

See this answer regarding request.data:

request.data Contains the incoming request data as string in case it came with a mimetype Flask does not handle.

The recommended way to handle file uploads in flask is to use:

file = request.files['file']
  • file is then of type: werkzeug.datastructures.FileStorage.

  • file.stream is the stream, which can be read with file.stream.read() or simply file.read()

  • file.filename is the filename as specified on the client.

  • file.save(path) a method which saves the file to disk. path should be a string like '/some/location/file.ext'

source

v25
  • 7,096
  • 2
  • 20
  • 36