【问题标题】:Wring file retrieved using Blobstore使用 Blobstore 检索到的 Wring 文件
【发布时间】:2011-04-09 07:04:06
【问题描述】:

我有表单上传和处理程序,允许从 blobstore 下载上传的文件。 问题是当我单击任何相关字段的下载按钮时,它每次都会下载相同的文件。 IE。我上传了 3 个文件(1.txt、2.txt、3.txt),每当我点击另一个下载按钮时,它总是只下载 1.txt。你可以在http://my77notes.appspot.com/show 看到它(或http://my77notes.appspot.com/upload 首先上传你自己的文件)。 当我研究源代码时,它向我展示了每个隐藏字段的不同键.. 我做错了什么?

这是我的文件:

模板文件:

<h2>Files uploaded to Blobstore</h2>
<table border="3">
    <tr>
        <td>#</td>
        <td>Filename</td>
        <td>Content-Type</td>
        <td>Creation</td>
        <td>Size</td>
        <td>Download</td>
    </tr>
<form id="show_blob" name="show_blob" method="post" action="{{ download_blob }}">
    {% for file in blob_files %}
    <tr>
        <td>{{ loop.index }}</td>
        <td>{{ file.filename }}</td>
        <td>{{ file.content_type }}</td>
        <td>{{ file.creation }}</td>
        <td>{{ file.size }}</td>
        <td>
            <input type="submit" name="download" value="Download"/>
            <input type="hidden" name="blobkey" value="{{ file.key() }}" />
        </td>
    </tr>
    {% endfor %}
</form>
</table>

handler.py

class BlobstoreServeHandler(RequestHandler, BlobstoreDownloadMixin):
    def post(self):
        blob_info = blobstore.BlobInfo.get(self.request.form.get('blobkey'))
        return self.send_blob(blob_info, save_as=True)

urls.py

rules = [
        Rule('/', endpoint='index', handler='apps.77notes.handlers.IndexPageHandler'),
        Rule('/upload', endpoint='upload/html', handler = 'apps.77notes.handlers.BlobstoreUploadFormHandler'),
        Rule('/upload/handler', endpoint='upload/handler', handler='apps.77notes.handlers.UploadHandler'),
        Rule('/download', endpoint='download/html', handler = 'apps.77notes.handlers.BlobstoreDownloadFormHandler'),
        Rule('/download/file', endpoint='download/file', handler='apps.77notes.handlers.BlobstoreServeHandler'),
        Rule('/show', endpoint='show/html', handler='apps.77notes.handlers.ShowUploadedFilesHandler'),
]

变量

blob_files = uploaded_files_to_blobstore = blobstore.BlobInfo.all()
download_blob = self.url_for('download/file')

谢谢!

【问题讨论】:

    标签: python jinja2 blobstore tipfy


    【解决方案1】:

    当然,它总是第一个。您正在声明三个具有相同名称但值不同的隐藏字段。服务器怎么会理解你想要“离我点击的下载按钮最近的隐藏字段”?

    您可以使用 Javascript 来做到这一点,但这有点矫枉过正。也许您应该为每个项目创建表单,但我不确定它是否是 HTML 有效的。

    {% for file in blob_files %}
    <tr>
        <!-- stuff -->
        <td><form class="show_blob" name="show_blob" method="post" action="{{ download_blob }}">
            <input type="submit" name="download" value="Download" />
            <input type="hidden" name="blobkey" value="{{ file.key() }}" />
        </form></td>
    </tr>
    {% endfor %}
    

    如果您不喜欢这样,您还可以在下载提交按钮中提供所需 blobkey 的索引。像这样的:

    {% for file in blob_files %}
    <tr>
        <!-- stuff -->
        <td>
            <input type="submit" name="dl{{ loop.counter0 }}" value="Download" />
            <input type="hidden" name="blobkey" value="{{ file.key() }}" />
        </td>
    </tr>
    {% endfor %}
    

    然后,在服务器端,您可以使用以下方法获得正确的 blobkey:

    # don't forget to handle errors here, NTUI
    ind = int([_[2:] for _ in self.request.form if _.startswith('dl')][0])
    blobkeys = self.request.form.getlist('blobkey')
    blobkey = blobkeys[ind]
    
    # stuff
    

    【讨论】:

    • ind = int([_[2:] for _ in self.request.form if _.startswith('dl')][0]) - 你确定它是有效的代码吗?
    【解决方案2】:

    如果你通过表单下载什么,你需要做尽可能多的表单

    {% for file in blob_files %}
    <tr>
        <td>{{ loop.index }}</td>
        <td>{{ file.filename }}</td>
        <td>{{ file.content_type }}</td>
        <td>{{ file.creation }}</td>
        <td>{{ file.size }}</td>
        <td>
            <form id="show_blob" name="show_blob" method="post" action="{{ download_blob }}">
            <input type="submit" name="download" value="Download"/>
            <input type="hidden" name="blobkey" value="{{ file.key() }}" />
            </form>
        </td>
    </tr>
    {% endfor %}
    

    或者你可以通过像这样的普通A标签&lt;a href = '/get/{{ file.key() }}'&gt;

    【讨论】:

      猜你喜欢
      • 2012-05-15
      • 1970-01-01
      • 2015-06-18
      • 2011-06-06
      • 1970-01-01
      • 2017-06-15
      • 2015-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多