如何手动处理上传的文件数据? [英] How do I deal with the uploaded file data manually?
问题描述
我正在尝试手动保存上载的文件以供学习,而无需 request.files
的帮助.
I am trying to save the uploaded file manually for learning, without the help of request.files
.
我正在使用Flask,并且通过 print(type(reqeust.data))
I am using Flask and it's giving me a str
type of the raw body data, by print(type(reqeust.data))
- 我很困惑.我是否应该获取二进制数据
< type bytes>
?
但是然后我再想一想,即使我获得了二进制数据,也该如何过滤前几行,然后从正确的位置开始读取二进制数据?
But then I think again, even if I get binary data, how can I filter the first several lines and then start to read the binary data from the right place?
例如:
-----------------------------1699415032232102060211780227
Content-Disposition: form-data; name="myfile"; filename="Screenshot from 2018-10-05 15-49-07.png"
Content-Type: image/png
�PNG
�ߧd�tEXtSoftwaregnome-screenshot��>�IDATx���OPY����l�*c���=��El"f[��)3��S�+z-v�0�c������zp����6��qS�\W��6S�qM�S=tG�Ǩb��A�ؒvc���@rh��.N]���?JK����b+�J��(�����OR�T
-----------------------------1699415032232102060211780227--
- 有人可以教我如何手动保存文件数据吗?
推荐答案
最后,我自己弄清楚了.
Finally I figured it out by myself.
-
我从
request.data
中获取str
的原因是因为我使用的是python2.7
flask软件包.这么痛的屁股.我肯定会在下次甚至使用virtualenv
进行测试.但是我仍然不明白为什么当软件包与python2.7有关时,为什么我可以在服务器代码中使用python3语法.
The reason why I am getting
str
fromrequest.data
is because I was usingpython2.7
flask packages. Such a pain in the ass. I would definitely usevirtualenv
next time, even for testing. But I still don't understand why I can use python3 syntax in the server code while the package is related to python2.7.
实际上,我实际上是从 request.data
获得原始字节数据,例如 b'raw binary data'
.以及来自其他功能的数据像 request.form ['firstname']
这样的代码已经被解码.
So actually I am getting raw byte data like b'raw binary data'
from request.data
. And data from other functions e.g. like request.form['firstname']
have already been decoded.
因此,现在的问题归结为我如何在提供二进制数据的情况下重建文件.
So now the question boilds down to how I can rebuild the file, provided the binary data.
首先,我准备2个小文件进行测试.
Af first, I prepare 2 small files for testing.
文件1: 1.txt
内容: 1234567
file2: test.png
这个小图片------>
file2: test.png
this small image ------>
内容(使用 open('test.png','rb').read()
):
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\t\x00\x00\x00\x08\x08\x02\x00\x00\x00\xa4\xafB\xe2\x00\x00\x00\x03sBIT\x08\x08\x08\xdb\xe1O\xe0\x00\x00\x00\x10tEXtSoftware\x00Shutterc\x82\xd0\t\x00\x00\x00\x15IDAT\x08\xd7c\xd4\xe5Tb\xc0\x01\x98\x18p\x83\xa1"\x07\x00T;\x00h\xb9\x9335\x00\x00\x00\x00IEND\xaeB`\x82'
所以我在服务器上看到的 request.data
是:
So the request.data
I see on the server is:
b'-----------------------------16866548741414816351605255076\r\nContent-Disposition: form-data; name="myfile"; filename="1.txt"\r\nContent-Type: text/plain\r\n\r\n1234567\r\n-----------------------------16866548741414816351605255076\r\nContent-Disposition: form-data; name="myfile2"; filename="test.png"\r\nContent-Type: image/png\r\n\r\n\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\t\x00\x00\x00\x08\x08\x02\x00\x00\x00\xa4\xafB\xe2\x00\x00\x00\x03sBIT\x08\x08\x08\xdb\xe1O\xe0\x00\x00\x00\x10tEXtSoftware\x00Shutterc\x82\xd0\t\x00\x00\x00\x15IDAT\x08\xd7c\xd4\xe5Tb\xc0\x01\x98\x18p\x83\xa1"\x07\x00T;\x00h\xb9\x9335\x00\x00\x00\x00IEND\xaeB`\x82\r\n-----------------------------16866548741414816351605255076--\r\n'
稍微格式化一下:
(数据不能直接使用,因为我添加了额外的新行来显示.)
(The data can't be used directly because I added extra new lines for displaying.)
b'-----------------------------16866548741414816351605255076\r\n
Content-Disposition: form-data; name="myfile"; filename="1.txt"\r\n
Content-Type: text/plain\r\n\r\n
1234567\r\n
-----------------------------16866548741414816351605255076\r\n
Content-Disposition: form-data; name="myfile2"; filename="test.png"\r\n
Content-Type: image/png\r\n\r\n
\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\t\x00\x00\x00\x08\x08\x02\x00\x00\x00\xa4\xafB\xe2\x00\x00\x00\x03sBIT\x08\x08\x08\xdb\xe1O\xe0\x00\x00\x00\x10tEXtSoftware\x00Shutterc\x82\xd0\t\x00\x00\x00\x15IDAT\x08\xd7c\xd4\xe5Tb\xc0\x01\x98\x18p\x83\xa1"\x07\x00T;\x00h\xb9\x9335\x00\x00\x00\x00IEND\xaeB`\x82\r\n
-----------------------------16866548741414816351605255076--\r\n'
让 raw_data =上面的二进制数据
-
files_data_array = raw_data.split(b'----------------------------- 16866548741414816351605255076 \ r \ n)
然后我得到一个数组,其中每个文件的索引不同.
Then I get an array with each files in different index.
此处 files_data_array [1]
包含第一个文件元信息和数据. files_data_array [2]
包含第二个文件的元信息和数据.等等,如果您有更多文件.
Here files_data_array[1]
contains the first file meta info and data. files_data_array[2]
contains the second file meta info and data. And so on if you have more files.
[b'', b'Content-Disposition: form-data; name="myfile"; filename="1.txt"\r\nContent-Type: text/plain\r\n\r\n1234567\r\n', b'Content-Disposition: form-data; name="myfile2"; filename="test.png"\r\nContent-Type: image/png\r\n\r\n\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\t\x00\x00\x00\x08\x08\x02\x00\x00\x00\xa4\xafB\xe2\x00\x00\x00\x03sBIT\x08\x08\x08\xdb\xe1O\xe0\x00\x00\x00\x10tEXtSoftware\x00Shutterc\x82\xd0\t\x00\x00\x00\x15IDAT\x08\xd7c\xd4\xe5Tb\xc0\x01\x98\x18p\x83\xa1"\x07\x00T;\x00h\xb9\x9335\x00\x00\x00\x00IEND\xaeB`\x82\r\n-----------------------------16866548741414816351605255076--\r\n']
-
file2_data = files_data_array [2]
b'Content-Disposition: form-data; name="myfile2"; filename="test.png"\r\nContent-Type: image/png\r\n\r\n\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\t\x00\x00\x00\x08\x08\x02\x00\x00\x00\xa4\xafB\xe2\x00\x00\x00\x03sBIT\x08\x08\x08\xdb\xe1O\xe0\x00\x00\x00\x10tEXtSoftware\x00Shutterc\x82\xd0\t\x00\x00\x00\x15IDAT\x08\xd7c\xd4\xe5Tb\xc0\x01\x98\x18p\x83\xa1"\x07\x00T;\x00h\xb9\x9335\x00\x00\x00\x00IEND\xaeB`\x82\r\n-----------------------------16866548741414816351605255076--\r\n'
然后按 file2_meta_info = file2_data.split(b'\ r \ n \ r \ n',maxsplit = 1)[0]
拆分元数据.请注意,这里我正在分割二进制数据,并且如果文件数据中有 b'\ r \ n \ r \ n'
,则必须设置 maxsplit
.
Then split the meta by file2_meta_info = file2_data.split(b'\r\n\r\n', maxsplit=1)[0]
.
Notice that here I am splitting binary data, and in case there are b'\r\n\r\n'
in the file data, settting maxsplit
is necessary.
现在我得到 file2_meta_info
作为 b'Content-Disposition:form-data;name ="myfile";filename ="1.txt"'
,我可以对其进行解码并获取所需的任何元信息.
Now I get file2_meta_info
as b'Content-Disposition: form-data; name="myfile"; filename="1.txt"'
, I can decode it and get whatever meta info I want.
现在转到文件主体数据本身, file2_body_data = file2_data.split(b'\ r \ n \ r \ n',maxsplit = 1)[1]
Now turn to the file body data itself, file2_body_data = file2_data.split(b'\r\n\r\n', maxsplit=1)[1]
我知道
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\t\x00\x00\x00\x08\x08\x02\x00\x00\x00\xa4\xafB\xe2\x00\x00\x00\x03sBIT\x08\x08\x08\xdb\xe1O\xe0\x00\x00\x00\x10tEXtSoftware\x00Shutterc\x82\xd0\t\x00\x00\x00\x15IDAT\x08\xd7c\xd4\xe5Tb\xc0\x01\x98\x18p\x83\xa1"\x07\x00T;\x00h\xb9\x9335\x00\x00\x00\x00IEND\xaeB`\x82\r\n-----------------------------16866548741414816351605255076--\r\n'
与开头显示的 test.png
的内容相比,我仍然需要削减一些字节
I still need to cut some bytes, compared to the content of test.png
showed at the beginning
real_file2_body_data = file2_body_data.rsplit(b'\ r \ n',maxsplit = 2)[0]'
最后我可以使用以下命令重新建立文件:
Finally I can rebuid the file with :
f = open('test2.png', 'wb')
f.write(real_file2_body_data)
f.close()
完成!
这篇关于如何手动处理上传的文件数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!