简体   繁体   English

google appengine-让用户下载文件

[英]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. 即使我尝试了chmod a+r file.pdf请帮助。 Thank you! 谢谢!

os.path.dirname(__file__) gives you the application directory. os.path.dirname(__file__)提供了应用程序目录。

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

Store your file in BP/ folder inside the main folder. 将文件存储在主文件夹内的BP /文件夹中。

@Nicke, @Jon: GAE allows access to your files. @ Nicke,@ Jon:GAE允许访问您的文件。 "May the source be with you". “愿消息来源与您同在”。

GAE doesn't have access to files. GAE无权访问文件。 If you want to serve a file, serve it from a static directory or from the blobstore. 如果要提供文件,请从静态目录或blobstore提供文件。

The user running AppEngine does not have read access to the directory. 运行AppEngine的用户没有对该目录的读取权限。 Try to chmod the BP dir. 尝试更改BP目录。

But even so this will not work once you deploy your app. 但是即使如此,一旦部署应用程序,这将无法正常工作。 There is no notion of a file system in App Engine. App Engine中没有文件系统的概念。 May I suggest you store the file in a blob or in the data store. 我可以建议您将文件存储在Blob还是数据存储中。 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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM