使用python将图像上载到文件夹
我想浏览图像并使用python将其上载到文件夹。我尝试过论坛上发布的各种解决方案,但没有一个对我有效。请指导我哪些地方需要纠正。谢谢大家的快速帮助 我犯了个错误 提高属性错误属性 AttributeError:具有\u键使用python将图像上载到文件夹,python,google-app-engine,app-engine-ndb,Python,Google App Engine,App Engine Ndb,我想浏览图像并使用python将其上载到文件夹。我尝试过论坛上发布的各种解决方案,但没有一个对我有效。请指导我哪些地方需要纠正。谢谢大家的快速帮助 我犯了个错误 提高属性错误属性 AttributeError:具有\u键 #!/usr/bin/env python import cgi, os import cgitb; cgitb.enable() import cgi import datetime import webapp2 import cgi, os import cgitb; c
#!/usr/bin/env python
import cgi, os
import cgitb; cgitb.enable()
import cgi
import datetime
import webapp2
import cgi, os
import cgitb; cgitb.enable()
from google.appengine.ext import ndb
from google.appengine.api import users
guestbook_key = ndb.Key('Guestbook', 'default_guestbook')
class Greeting(ndb.Model):
author = ndb.UserProperty()
content = ndb.TextProperty()
date = ndb.DateTimeProperty(auto_now_add=True)
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.out.write('<html><body>')
greetings = ndb.gql('SELECT * '
'FROM Greeting '
'WHERE ANCESTOR IS :1 '
'ORDER BY date DESC LIMIT 10',
guestbook_key)
for greeting in greetings:
if greeting.author:
self.response.out.write('<b>%s</b> wrote:' % greeting.author.nickname())
else:
self.response.out.write('An anonymous person wrote:')
self.response.out.write('<blockquote>%s</blockquote>' %
cgi.escape(greeting.content))
self.response.out.write("""
<form enctype="multipart/form-data" action="/sign" method="post">
<p>File: <input type="file" name="file1"></p>
<p><input type="submit" value="Upload"></p>
</form>
</html>""")
class Guestbook(webapp2.RequestHandler):
def post(self):
form = cgi.FieldStorage()
# A nested FieldStorage instance holds the file
#file = models.FileField(upload_to='documents/', max_length=5234,blank=True, null=True,)
# docfile = forms.FileField(label='', show_hidden_initial='none',required=True,)
fileitem = str(self.request.get('file1'))
# Test if the file was uploaded
if self.request.has_key('file1'):
# strip leading path from file name to avoid directory traversal attacks
fn = os.path.basename(fileitem.file)
open('files/' + fn, 'wb').write(fileitem.file.read())
message = 'The file "' + fn + '" was uploaded successfully'
else:
message = 'No file was uploaded'
print """\
Content-Type: text/html\n
<html><body>
<p>%s</p>
</body></html>
""" % (message,)
app = webapp2.WSGIApplication([
('/', MainPage),
('/sign', Guestbook)
], debug=True)
您需要停在这里,回头阅读appengine和python运行时的介绍性文档。如果您通读介绍文档,您将看到关于python运行时和沙箱及其限制的部分 在检查文档的这一部分时,您将看到无法在appengine中写入文件系统。同时,还值得注意的是,在您进行此操作时,还有其他限制 至于您的错误在代码中的什么位置,您至少应该包括一个stacktrace,并查看错误发生的特定代码行,然后询问有关该错误的具体问题,而不是转储所有代码并说出您遇到了什么错误
目前,对于代码中出现has_key错误的问题,我看不出有什么意义,该错误是不言自明的,您尝试执行的其他操作无论如何都不会起作用。您需要停在这里,回头阅读appengine和python运行时的介绍性文档。如果您通读介绍文档,您将看到关于python运行时和沙箱及其限制的部分 在检查文档的这一部分时,您将看到无法在appengine中写入文件系统。同时,还值得注意的是,在您进行此操作时,还有其他限制 至于您的错误在代码中的什么位置,您至少应该包括一个stacktrace,并查看错误发生的特定代码行,然后询问有关该错误的具体问题,而不是转储所有代码并说出您遇到了什么错误
目前,对于代码中出现has_键错误的问题,我看不出有什么意义,该错误是不言自明的,您尝试执行的其他操作无论如何都不会起作用。您的GAE Python项目文件是只读的。只有在使用appcfg.py或推送部署更新项目时,才能更改这些文件 但您可以使用Google cloudstorage文件夹或子目录上载、写入或覆盖文件。 文件:
如果对文件夹使用appid默认存储桶,则有5 GB的可用配额。您的GAE Python项目文件是只读的。只有在使用appcfg.py或推送部署更新项目时,才能更改这些文件 但您可以使用Google cloudstorage文件夹或子目录上载、写入或覆盖文件。 文件: 如果为文件夹使用appid默认存储桶,则有5 GB的可用配额