views:

391

answers:

2

I'm trying to associate a video file to a record with a bunch of properties, but can't seem to allow the user to do everything in one form - name the video, provide description and answer some question, AND upload the file.

Here are the steps I'd like to perform:

  1. User is served with a page containing a form with the following fields: Name, Description, File selector.
  2. The file gets stored as a blob and the id gets recorded together with name and description.

Does anyone have any examples of doing this I could learn from or a tutorial you could point me to? The one from google only shows uploading the file and getting redirected to it.

Thanks and sorry for a newbish question!

+2  A: 

Here's the code I'm using to upload images and associate them with articles. The toughest bit was getting the article id to get to the upload handler, I solved it by setting the file name as the article id to get around the problem.

from lib import urllib2_file
from lib.urllib2_file import UploadFile

# this view serves a task in a queue
def article(request):
       article = Article.objects.get(id=form.cleaned_data['article'])

       try:
            image = StringIO(urllib2.urlopen(image_url).read())
        except (urllib2.HTTPError, DownloadError):
            article.parsed = True
            article.save()
        else:
            image = UploadFile(image, '.'.join([str(article.id), image_url.rsplit('.', 1)[1][:4]]))
            upload_url = blobstore.create_upload_url(reverse('Articles.views.upload'))

            try:
                urllib2.urlopen(upload_url, {'file': image})
            except (DownloadError, RequestTooLargeError):
                pass

    return HttpResponse(json.dumps({'status': 'OK'}))

def upload(request):
    if request.method == 'POST':
        blobs = get_uploads(request, field_name='file', populate_post=True)

        article = Article.objects.get(id=int(blobs[0].filename.split('.')[0]))
        article.media = blobs[0].filename
        article.parsed = True
        article.save()

        return HttpResponseRedirect(reverse('Articles.views.upload'))
    else:
        return HttpResponse('meow')

    def upload(request):
        if request.method == 'POST':
            blobs = get_uploads(request, field_name='file', populate_post=True)

            article = Article.objects.get(id=int(blobs[0].filename.split('.')[0]))
            article.media = blobs[0].filename
            article.parsed = True
            article.save()

            return HttpResponseRedirect(reverse('Articles.views.upload'))
        else:
            return HttpResponse('meow')

# this serves the image
def image(request):
    blob = BlobInfo.gql("WHERE filename='%s' LIMIT 1" % request.form.cleaned_data['id'])[0]

    return HttpResponse(BlobReader(blob.key()).read(),
                        content_type=blob.content_type)

Also you'll need this http://fabien.seisen.org/python/urllib2_file/

Swizec Teller
Thanks! Can you give me a little more context for this (more noob questions) - are you using webapp or django?
Sologoub
I'm using django-nonrel. Since I'm already used to django that was the easiest way to go about doing this because I didn't have to learn webapp. Also there are some imports and such things missing from the code I pasted, tried to give just the relevant bits :)
Swizec Teller
Sounds good. Thank you! Since I lack experience, I just wanted to make sure I could reference some extra docs to better understand it.
Sologoub
+2  A: 

http://demofileuploadgae.appspot.com/ - My demo uploader to the blobstore.

My code for the upload: http://code.google.com/p/gwt-examples/source/browse/trunk/DemoUpload/src/org/gonevertical/upload/#upload/server%3Fstate%3Dclosed

Thank you very much!
Sologoub
By the way, to clarify for those looking at these examples, this one is JAVA
Sologoub