Google App Engine(Python)获取文件内容

时间:2013-12-01 04:41:57

标签: python google-app-engine

我正在编写一个Web应用程序,我需要阅读用户将上传的文本文件。

我不需要将它存储在数据库中;应用程序仅对文件中的文本执行简单操作。

我目前正在使用< input type = file“name =”input_file“id =”file-in“>上传文件,该表格通过POST提交并在main.py中运行:

user_file = self.request.get("input_file")
print user_file
print type(user_file)

当我尝试上传文件(“foo.txt”)时输出内容为“快速的棕色狐狸跳过懒狗!”:

foo.txt
<type 'unicode'>

为什么会这样?如何访问文件的内容

感谢任何帮助。如果您需要一些背景信息,我写了this simple encryptor/decryptor app,我正在尝试添加上传文件的功能,而不是仅仅复制粘贴文本。

2 个答案:

答案 0 :(得分:1)

为您的input命名:

<input type="file" name="input_file"/> 

然后在您对input的调用中使用名称而不是request.get()

user_file = self.request.get("input_file")

另请参阅:Upload files in Google App Engine

答案 1 :(得分:0)

尝试:

user_file = self.request.get("input")
print user_file
print type(user_file)

print user_file.decode('utf-8')
print type(user_file.decode('utf-8'))