Code samples for Django + SWFUpload?

Deadly 提交于 2019-11-28 20:41:33

问题


Does anyone have any simple code samples for Django + SWFUpload? I have it working perfectly in my PHP application but Django is giving me headaches.


回答1:


Unfortunately I can't give you any very detailed code samples, but I have quite a bit of experience with working with SWFUpload + Django (for a photo sharing site I work on). Anyway, here are a few pointers that will hopefully help you on your quest for DjSWF happiness :)

  1. You'll want to use the cookies plugin (if of course you are using some sort of session-based authentication [like django.contrib.auth, and care who uploaded what).

    The cookies plugin sends the data from cookies as POST, so you'll have to find some way of getting this back into request.COOKIES (process_request middleware that looks for a settings.SESSION_COOKIE_NAME in request.POST on specific URLs and dumps it into request.COOKIES works nicely for this :)

  2. Also, remember that you must return something in the response body for SWFUpload to recognize it as a successful upload attempt. I believe this has changed in the latest beta of SWFUpload, but anyway it's advisable just to stick something in there like 'ok'. For failures, make use of something like HttpResponseBadRequest or the like.

  3. Lastly, in case you're having trouble finding them, the uploaded file is in request.FILES :)

If you have anything perplexing I haven't covered, feel free to post something more detailed and I'll be happy to help.




回答2:


Django version of the samples for SWFUpload:

http://github.com/naltimari/django-swfupload-samples/tree/master

So long uploadify. Great idea but it is just buggy, especially on Windows.




回答3:


The following is my Django-specific implementation for fixing this issue (i.e. my uploads were failing in Firefox with a 302 Redirect).

In my initial view that generates the page with the uploader on it, I looked at the cookies and found sessionid

ipdb> self.request.COOKIES
{'csrftoken': '43535f552b7c94563ada784f4d469acf', 'sessionid': 'rii380947wteuevuus0i5nbvpc6qq7i1'}

When I looked at what was being posted in the SWFUploadMiddleware (when using Firefox), I found that the sessionid was not being set.

In my intial view that generates the page that contains the upload handler, I added the sessionid to the context.

context['sessionid'] = self.request.session.session_key

In my swfuploader settings, I added sessionid to the post-params option as follows:

post_params: {... "sessionid": "{{ sessionid }}" ...},

Now, when I looked in the SWFUploadMiddleware, I could see the sessionid being posted, and my uploads started working if Firefox.

ipdb> request.POST 
<QueryDict: {... u'session_id': [u'rii380947wteuevuus0i5nbvpc6qq7i1'],...}>

For completeness, my SWFUploadMiddleware looks like this...

from django.conf import settings
from django.core.urlresolvers import reverse

class SWFUploadMiddleware(object):
    def process_request(self, request):
        if (request.method == 'POST') and (request.path == reverse('upload_handler')) and request.POST.has_key(settings.SESSION_COOKIE_NAME):
            request.COOKIES[settings.SESSION_COOKIE_NAME] = request.POST[settings.SESSION_COOKIE_NAME]
    # http://stackoverflow.com/questions/6634666/403-forbidden-error-on-swfupload-and-django
    # Fix for problem uploading images (403 error) in Firefox 20 and others
    if request.POST.has_key('csrftoken'):
            request.COOKIES['csrftoken'] = request.POST['csrftoken']


来源:https://stackoverflow.com/questions/612734/code-samples-for-django-swfupload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!