Flask:获取从邮递员发送的gzip文件名 [英] Flask: Get gzip filename sent from Postman
问题描述
我正在从Postman发送一个gzip文件到Flask端点。我可以使用 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".
我该如何取这些名字?
编辑:
我正在使用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'\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'
推荐答案
在此之前,我认为处理您上传的代码在这里很重要。
Further to the comment, I think the code which handles your upload is relevant here.
关于 request.data
,请参见此答案。 :
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
的类型为:werkzeug.datastructures.FileStorage
。file
is then of type:werkzeug.datastructures.FileStorage
.file.stream
是流,可以用<$ c $读取c> file.stream.read()或简单地file.read()
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屋!