如何在Python App Engine中使用BlobStore上传文件? [英] How to upload a file using the BlobStore in Python App Engine?

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

问题描述

我做了app引擎文档中指定的所有操作,但无法获得blobstore的工作.也许有些人可以发现我在做什么错.当我单击提交"按钮

I did everything specified in the documentation of app engine but i couldn't get the blobstore work. Maybe some of you can detect what i am doing wrong. When i clicked the submit button

这种网址出现在地址栏中,空白的白页出现在我的面前.

This kind of a url is seen in the address bar and an empty white page is in front of me.

http://localhost:8080/_ah/upload/agltb2JpbHNvcnVyGwsSFV9fQmxvYlVwbG9hZFNlc3Npb25fXxg9DA

有人建议吗?

这些是我的处理程序:

class MainHandler(webapp.RequestHandler):
  def get(self):
    years = Years.all().order("Year")
    months = Months.all().order("SortNumber")

    upload_url = blobstore.create_upload_url('/imidergi/upload')

    content = {
        'upload': upload_url,
        'yearList':years,
        'monthList':months,
        }

    render_template(self, 'imidergi.html', content)

class AddDergi(blobstore_handlers.BlobstoreUploadHandler):
  def post(self):
    # 'file' is file upload field in the form
    upload_files = self.get_uploads('file')
    blob_info = upload_files[0]

    dergi = Dergiler()
    dergi.Year = self.request.get("yil")
    dergi.Month = self.request.get("ay")
    dergi.DergiPDF = str(blob_info.key())
    dergi.Name = self.request.get("isim")
    dergi.Image = self.request.get("resim")
    dergi.put()

    self.response.out.write(dergi.Name)

这是呈现表单的html.

And this is the html which renders the form.

<form action="{{ upload }}" method="post" id="dergiform" enctype="multipart/form-data">
  {{ upload }}
  <label>Yil:</label><select name="yil">
  {% for year in yearList %}
    <option value="{{ year.Year }}">{{ year.Year }}</option>
  {% endfor %}
  </select><br/>
  <label>Ay:</label><select name="ay">
  {% for month in monthList %}
    <option value="{{ month.Name }}">{{ month.Name }}</option>
  {% endfor %}
  </select><br/>

  <label>Isim: </label><input type='text' id="isim" name="isim"/><br/>
  <label>Dergi: </label><input type='file' id="file" name="file"/><br/>
  <label>Resim: </label><input type='file' id="resim" name="resim"/><br/>
  <label></label><input type='submit' value='Ekle'/>
</form>

推荐答案

IIRC BlobstoreUploadHandler期望您在处理完POST后返回重定向,因为您的处理程序确实在响应特殊的BlobStore像在正常请求中一样,直接通过客户端/浏览器上传服务器,而不是.

IIRC BlobstoreUploadHandler expects you to return a redirect after you have handled the POST as your handler is really responding to the special BlobStore upload servers and not directly with the client/browser like in a normal request.

blobstore 文档中复制示例,并且请记住,您只能 使用标头(例如重定向)而不是正文内容进行响应.

Copy the example from the blobstore docs, and remember that you can only respond with headers (like redirects) and not with body content.

这篇关于如何在Python App Engine中使用BlobStore上传文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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