gpt4 book ai didi

python - google appengine - 让用户下载文件

转载 作者:太空宇宙 更新时间:2023-11-04 07:45:52 24 4
gpt4 key购买 nike

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)

我在尝试下载时遇到此错误:

[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请帮助。谢谢!

最佳答案

os.path.dirname(__file__) 为您提供应用程序目录。

f = open(os.path.dirname(__file__) +/'BP/file.pdf', r)

将您的文件存储在主文件夹内的 BP/文件夹中。

@Nicke、@Jon:GAE 允许访问您的文件。 “愿源与你同在”。

关于python - google appengine - 让用户下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8178841/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com