class PDF(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = "application/txt"
self.response.headers['Content-Disposition'] = "attachment; filename=file.pdf"
f = open('/Users/BP/file.pdf', 'r')
self.response.out.write(f.read())
def main():
application = webapp.WSGIApplication([('/download', PDF)],
debug=False)
util.run_wsgi_app(application)
我得到這個錯誤,當我嘗試下載:谷歌AppEngine上 - 讓用戶下載文件
[Errno 13] Permission denied: '/Users/BP/file.pdf'
Traceback (most recent call last):
File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/_webapp25.py", line 701, in __call__
handler.get(*groups)
File "/base/data/home/apps/s~projectname/1.354763951774324185/main.py", line 43, in get
f = open('/Users/BP/file.pdf', 'r')
IOError: [Errno 13] Permission denied: '/Users/BP/file.pdf'
即使我已經試過chmod a+r file.pdf
請幫助。謝謝!
你是對的,它的作品像一個魅力:)謝謝! – BPm
GAE中的「正確」方式是在app.yaml中使用靜態文件句柄,如果您喜歡DIY風格,請嘗試使用[Webob](http://docs.webob.org/zh/latest/file-example.html )更有效的例子。 –
我看到有些框架會這樣做,我不確定建議我們在做WSGI時使用'os'module。當然,因爲在UNIX中「一切都是文件」,那麼你自然說我在這裏錯了。而且,如果你想使用os模塊,我不知道它是如何與WSGi同向的。如果你正在編程WSGI,我不確定是否推薦使用'os'模塊。 –