Flask:获取邮递员发送的 gzip 文件名 [英] Flask: Get gzip filename sent from Postman
问题描述
我正在从 Postman 向 Flask 端点发送一个 gzip 文件.我可以使用 request.data
获取该二进制文件并读取、保存、上传等.
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?
我的 gzip 文件名为test_file.json.gz",我的文件名为test_file.json".
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?
我正在使用 io.BytesIO() 获取流数据,但是这个库不包含 name 属性或其他东西,尽管我可以在字符串中看到文件名:
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.
关于request.data
,请参阅这个答案:
request.data
包含传入的请求数据作为字符串,以防它带有 Flask 无法处理的 mimetype.
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
然后是类型:werkzeug.datastructures.FileStorage
.file
is then of type:werkzeug.datastructures.FileStorage
.file.stream
是流,可以用file.stream.read()
或简单的file.read()<读取/code>
file.stream
is the stream, which can be read withfile.stream.read()
or simplyfile.read()
file.filename
是客户端指定的文件名.file.filename
is the filename as specified on the client.file.save(path)
一种将文件保存到磁盘的方法.path
应该是一个类似'/some/location/file.ext'
file.save(path)
a method which saves the file to disk.path
should be a string like'/some/location/file.ext'
这篇关于Flask:获取邮递员发送的 gzip 文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!