Flask:获取从 Postman 发送的 gzip 文件名文件名、Flask、Postman、gzip

2023-09-08 08:48:48 作者:过了爱做梦的年纪

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?

flask 返回 gzip格式文件

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'x1fx8bx08x08xcaxb1xd3]x00x03test_file.jsonx00xabxe6RPPxcaNxad4Txb2RP*Kxcc)M5Txe2xaax05x00xc2x8bxb6;x16x00x00x00'

解决方案

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

 
精彩推荐
图片推荐