Open GoogleCodeExporter opened 8 years ago
Hopefully I haven't got the wrong end of the stick but ...
Calling the following:
<img src="/pages/file/agphZXAtc2FtcGxlchALEgpwYWdlc19maWxlGAUM/" alt="didnt
work" />
will use this
url(r'^pages/file/(?P<object_id>.+)/$', object_detail,
{'queryset': File.all(),
'template_name': 'pages_file_detail.html'},
name="pages_file_detail_view"),
which renders the response with the pages_file_detail template, which appears
to be an html file.
File upload/download is a little more involved in AppEngine as you have to save
the file into a db.BlobProperty - and
then provide a view specifically to download the file from the blob (setting
the content-type etc at the same time).
For the upload you can use a form with a forms.ImageField in it (say called
file_data) and save the uploaded file (from
request.FILES) into a model containing a BlobProperty (and maybe the
form.cleaned_data['file_data'].content_type as
well).
Something like:
if request.method == 'POST':
form = MyImageUploadForm( request.POST, request.FILES)
if form.is_valid():
themodel = MyImageModel()
themodel.data = form.cleaned_data['file'].read()
themodel.content_type = form.cleaned_data['file'].content_type
themodel.put()
else:
form = MyImageUploadForm
where MyImageModel looks something like the following (you could even add your
text description to this model).
class MyImageModel(db.Model):
data = db.BlobProperty()
content_type = db.StringProperty()
For the download you need to fetch the model by its key, and write the
blobproperty to the response after setting the
content-type, but only for requests for the actual image (not the image and the
text). Maybe you could map your
download to something like ..
pages/download/file/(?P<object_id>.+)/$
Hope this helps a little bit.
Original comment by rossdjo...@gmail.com
on 3 Nov 2009 at 4:30
Yes, this completely solves the problem!
I'm uploading via the admin now so I only needed the following changes:
urls.py:
---------
url(r'^pages/file/(?P<object_id>.+)/$', 'pages.views.download_file',
name="pages_file_download_view"),
views.py:
---------
def download_file(request, object_id):
file = get_object_or_404(File, object_id)
return HttpResponse(file.data,
content_type=guess_type(file.title)[0] or 'application/octet-stream')
and in models.py:
-----------------
@permalink
def get_absolute_url(self):
return ('pages_file_download_view', [self.key()])
Original comment by dstrijdh...@gmail.com
on 3 Nov 2009 at 11:49
Original issue reported on code.google.com by
dstrijdh...@gmail.com
on 3 Nov 2009 at 1:17