BPm
BPm

Reputation: 2994

google appengine - let user download a file

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!

Upvotes: 1

Views: 930

Answers (4)

sandeep koduri
sandeep koduri

Reputation: 510

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"

Upvotes: 0

Alvin K.
Alvin K.

Reputation: 4379

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".

Upvotes: 3

Jon Nylander
Jon Nylander

Reputation: 8963

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.

Upvotes: 2

Niklas Rosencrantz
Niklas Rosencrantz

Reputation: 26671

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

Upvotes: 2

Related Questions