【问题标题】:How to upload a file using the BlobStore in Python App Engine?如何使用 Python App Engine 中的 BlobStore 上传文件?
【发布时间】:2011-03-28 11:46:18
【问题描述】:

我完成了应用引擎文档中指定的所有操作,但无法让 blobstore 正常工作。也许你们中的一些人可以发现我做错了什么。当我点击提交按钮时

在地址栏中看到这种url,一个空白的页面在我面前。

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。

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

【问题讨论】:

  • 为什么将 blob 键存储为字符串?有一个专门用于此目的的 BlobRefProperty。

标签: google-app-engine blobstore


【解决方案1】:

IIRC BlobstoreUploadHandler 希望您在处理完 POST 后返回 redirect,因为您的处理程序实际上是在响应特殊的 BlobStore 上传服务器,而 直接使用像普通请求一样的客户端/浏览器。

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

【讨论】:

  • 感谢您的回答,但即使我使用了重定向它仍然无法正常工作。如果您可以提交我必须实现以使其工作的部分代码,那就太好了。还是谢谢你..
  • 您能否更新您的问题以反映您返回重定向的代码,我很乐意看看。 还请修正您的格式 - 代码需要有四个空格才能正确显示
  • “不起作用”怎么办?您是否查看过日志,是否使用过 chrome 开发者工具或 firebug 来查看返回的内容?
猜你喜欢
  • 2013-12-16
  • 1970-01-01
  • 1970-01-01
  • 2020-01-23
  • 1970-01-01
  • 1970-01-01
  • 2019-04-02
  • 2015-02-07
  • 2013-06-22
相关资源
最近更新 更多