Skip to main content
 首页 » 编程设计

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

2025年05月04日25Renyi-Fan

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 允许访问您的文件。 “愿源与你同在”。