在Google App Engine中上传文件并使其可下载 [英] Upload a file in Google App Engine and making it downloadable

查看:85
本文介绍了在Google App Engine中上传文件并使其可下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是google app引擎的新手,我想将其用作服务器来使人们下载文件.我已经看过python的教程.我没有找到任何实际指导我如何为此目的将文件上传到服务器的东西.

I am new with google app engine and I want to use it as a server to enable people download a file. I have gone through the tutorials in python. I didnt find anything to actually guide me how to upload files to the server for this purpose.

推荐答案

Blobstore教程仅给出了此用例的示例.该链接提供了以下代码:一个允许用户上传文件然后立即下载文件的应用程序:

The Blobstore tutorial gives an example for just this use case. That link provides this code: an application that lets users upload files and then immediately download them:

#!/usr/bin/env python
#

import os
import urllib

from google.appengine.ext import blobstore
from google.appengine.ext import webapp
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

class MainHandler(webapp.RequestHandler):
    def get(self):
        upload_url = blobstore.create_upload_url('/upload')
        self.response.out.write('<html><body>')
        self.response.out.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url)
        self.response.out.write("""Upload File: <input type="file" name="file"><br> <input type="submit" 
            name="submit" value="Submit"> </form></body></html>""")

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        upload_files = self.get_uploads('file')  # 'file' is file upload field in the form
        blob_info = upload_files[0]
        self.redirect('/serve/%s' % blob_info.key())

class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
    def get(self, resource):
        resource = str(urllib.unquote(resource))
        blob_info = blobstore.BlobInfo.get(resource)
        self.send_blob(blob_info)

def main():
    application = webapp.WSGIApplication(
          [('/', MainHandler),
           ('/upload', UploadHandler),
           ('/serve/([^/]+)?', ServeHandler),
          ], debug=True)
    run_wsgi_app(application)

if __name__ == '__main__':
  main()

这篇关于在Google App Engine中上传文件并使其可下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆