在GAE Appengine中用Python显示上传的文件 [英] Displaying uploaded file in GAE Appengine with Python
问题描述
我已经将数据存储中用户上传的pdf文件存储为BlobProperties:
< form action =/ method =postenctype =multipart / form-data>
< input type =filename =pdf>
< input type =submitvalue =上传>
< / form>
以下处理程序:
<$ p $如果p:
person.pdf = p
def post(self):
p = self.request.get('pdf')
我也尝试了最后一行的这个版本:
person.pdf = db.Blob(p)
看来以相同的方式工作。我尝试显示它:
< embed src ={{person.pdf}}width =500
height =375type =application / pdf>
与这个句柄:
def get(self,person_id):
person = Person.get_by_id(int(person_id))
self.response.headers ['Content-Type'] =application / pdf
template_values = {
'person':person
}
template = jinja_environment.get_template('person.html' )
self.response.out.write(template.render(template_values))
person.pdf的大小表明它确实包含整个pdf内容,但是在模板中,变量
{{person .pdf}}
会导致以下错误:
UnicodeDecodeError:'ascii'编解码器无法解码位置10中的字节0xe2:序号不在范围内(128)
所以没有显示。据我了解,Blobproperties存储为字符串。我错过了什么?
我终于通过简单的做法解决了这个问题:
self.response.write(person.pdf)
在显示处理程序中。 D'oh。
I have stored pdf files uploaded by users in the Datastore, as BlobProperties:
<form action="/" method="post" enctype="multipart/form-data">
<input type="file" name="pdf">
<input type="submit" value="Upload">
</form>
with the following handler:
def post(self):
p = self.request.get('pdf')
if p:
person.pdf = p
I have also tried this version of the last line:
person.pdf = db.Blob(p)
which seems to work the same way. I try to display it thus:
<embed src="{{ person.pdf }}" width="500"
height="375" type="application/pdf">
with this handler:
def get(self,person_id):
person = Person.get_by_id(int(person_id))
self.response.headers['Content-Type']="application/pdf"
template_values = {
'person' : person
}
template = jinja_environment.get_template('person.html')
self.response.out.write(template.render(template_values))
The size of person.pdf indicaties that it does indeed contain the entire pdf content, but in the template, the variable
{{ person.pdf }}
causes the following error:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 10: ordinal not in range(128)
so nothing is displayed. As far as I understand, Blobproperties are stored as strings. What am I missing?
I finally solved it by simply doing:
self.response.write(person.pdf)
in the display handler. D'oh.
这篇关于在GAE Appengine中用Python显示上传的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!