使用 Blobstore 检索 Wring 文件
我有表单上传和处理程序,允许从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然,它永远是第一。您正在声明三个具有相同名称但具有不同值的隐藏字段。服务器如何理解您想要“距离我单击的下载按钮最近的隐藏字段”?
你可以用 Javascript 来做到这一点,但它太过分了。也许您应该为每个项目创建表单,但我不确定它是否是 HTML 有效的。
如果您不喜欢这样,您还可以在下载提交按钮中提供所需 blobkey 的索引。像这样:
然后,在服务器端,您可以使用以下方法获得正确的 blobkey:
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.
If you don't like this, you could also provide the index of the desired blobkey within the download submit button. Something like this:
Then, server-side, you get the right blobkey using:
如果您要通过表单下载什么,您需要执行与您拥有的 blob 一样多的表单
,或者您可以通过像这样的普通 A 标记来完成
if you what to download through form you need to do as many form as blob you have
or you can do it by ordinary A tag like this
<a href = '/get/{{ file.key() }}'>