问题描述:

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)

I get this error when I try to download it:

[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'

Even though I've tried the chmod a+r file.pdf

Help please. Thank you!

网友答案:

os.path.dirname(__file__) gives you the application directory.

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

Store your file in BP/ folder inside the main folder.

@Nicke, @Jon: GAE allows access to your files. "May the source be with you".

网友答案:

GAE doesn't have access to files. If you want to serve a file, serve it from a static directory or from the blobstore.

网友答案:

The user running AppEngine does not have read access to the directory. Try to chmod the BP dir.

But even so this will not work once you deploy your app. There is no notion of a file system in App Engine. May I suggest you store the file in a blob or in the data store. Or static directory.

网友答案:

Can also serve static files, don't need a handler for this

- url: /sampleData\.csv
  static_files: sampleData.txt
  upload: sampleData\.csv
  http_headers:
    Content-Type: text/csv
    Content-Disposition: "attachment; filename=sample.csv"
相关阅读:
Top