使用 Blobstore 检索 Wring 文件

发布于 2024-10-31 10:52:13 字数 2602 浏览 0 评论 0原文

我有表单上传和处理程序,允许从 blobstore 下载上传的文件。 问题是,当我单击任何相关字段的“下载”按钮时,它每次都会下载相同的文件。即,我上传了 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')

谢谢!

I have form upload and handler which allows download uploaded files from blobstore.
The problem is when I click Download button of any related-field it downloads the same file every time. I.e. I've uploaded 3 files (1.txt, 2.txt, 3.txt) and it always downloads only 1.txt whenever I clicked another Download buttons. You can see it at http://my77notes.appspot.com/show (or http://my77notes.appspot.com/upload first for uploading your own files).
When I've researched source code it shows me different keys for every hidden fields..
What did I wrong?

Here is my files:

template file:

<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'),
]

variables

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

Thanks!

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

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

发布评论

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

评论(2

寻找一个思念的角度 2024-11-07 10:52:14

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

你可以用 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

Of course it's always the first. You're declaring three hidden fields with the same name but various values. How could the server understand you want “the hidden field nearest to the download button I clicked”?

You could do this with Javascript but it's overkill. Maybe you should rather create forms for each item, but I'm not sure it is HTML-valid.

{% 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 %}

If you don't like this, you could also provide the index of the desired blobkey within the download submit button. Something like this:

{% 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 %}

Then, server-side, you get the right blobkey using:

# 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
┊风居住的梦幻卍 2024-11-07 10:52:14

如果您要通过表单下载什么,您需要执行与您拥有的 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>
        <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 标记来完成

if you what to download through form you need to do as many form as blob you have

{% 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 %}

or you can do it by ordinary A tag like this <a href = '/get/{{ file.key() }}'>

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