Python 3.x 为什么读取上传文件和读取本地文件会产生不同的结果?
我将文件发布到python flask,然后使用以下代码读取其内容:Python 3.x 为什么读取上传文件和读取本地文件会产生不同的结果?,python-3.x,flask,Python 3.x,Flask,我将文件发布到python flask,然后使用以下代码读取其内容: def post(self): if 'file' not in request.files: return {'error': 'no file'} try: f=open("text2.txt") local_content=f.read() content=request.files['file'].rea
def post(self):
if 'file' not in request.files:
return {'error': 'no file'}
try:
f=open("text2.txt")
local_content=f.read()
content=request.files['file'].read().decode('utf-8')
if hash(content) != hash(local_content) :
return {'error': 'content changed','local':hash(local_content),'uploaded':hash(content)}
else:
return {'error': 'same','local':hash(local_content),'uploaded':hash(content)}
我还将相同的文件text2.txt放在服务器上,并使用
local_content=f.read()
但两个结果都不一样
if content != local_content
上述条件总是返回true。
但当我打印两个字符串时,它们是完全相同的
我正在对这些字符串进行处理并尝试,内容和本地内容都会产生不同的结果
那么,有人能告诉我为什么上传的内容与本地内容的行为不同吗?即使我在使用flask上传图像时遇到了这个问题。与读取本地文件相比,二进制流是不同的 我使用file.seek(0)解决此问题
files = request.files.getlist('files[]')
for file in files:
file.seek(0)
fileBytes = file.read()
image_stringRead = base64.b64encode(fileBytes)