如何使用 Python App Engine 中的 BlobStore 上传文件?

发布于 2024-10-26 16:52:57 字数 2092 浏览 2 评论 0原文

我执行了应用程序引擎文档中指定的所有操作,但无法让 blobstore 工作。也许你们中的一些人可以发现我做错了什么。当我单击“提交”按钮时,

地址栏中会看到这种网址,并且我面前是一个空白的页面。

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>

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

Does anyone have a suggestion?

These are my Handlers :

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)

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清引 2024-11-02 16:52:57

IIRC BlobstoreUploadHandler 希望您在处理 POST 后返回重定向,因为您的处理程序实际上是在响应特殊的 BlobStore 上传服务器,而不是直接响应像正常请求一样与客户端/浏览器进行交互。

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文