Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 views
in Technique[技术] by (71.8m points)

python 3.x - Flask: Get gzip filename sent from Postman

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'x1fx8bx08x08xcaxb1xd3]x00x03test_file.jsonx00xabxe6RPPxcaNxad4Txb2RP*Kxcc)M5Txe2xaax05x00xc2x8bxb6;x16x00x00x00'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...