使用Django上传多个文件 [英] Uploading multiple files with Django
问题描述
如何使用Django上传多个文件?
How do I upload multiple files with Django?
推荐答案
经过很多痛苦,我终于得到uploadify( http://www.uploadify.com/ )使用django,但问题不是真的django的,但得到它与苹果Mac的工作;该平台上的浏览器不会在Flash中提供Cookie;您需要手动设置它们:
After a lot of pain I eventually got uploadify (http://www.uploadify.com/) working with django, but the problem wasn't really django's, but getting it to work with Apple Mac's; browsers on that platform don't serve the cookies from within Flash; you need to set them manually:
所以我将这些包含在我的呈现请求中:
So I include these in my render-to-reponse:
return render_to_response('files_upload.html', {
'session_cookie_name': settings.SESSION_COOKIE_NAME,
'session_key': request.session.session_key
通过模板中定义的配置将其从uploadify呈现:
And present them from uploadify via the configuration laid down in the template:
$(document).ready(function() {
$('#fileInput').uploadify({
'scriptData': {'{{session_cookie_name}}': '{{session_key}}'},
在这个视图下,这是肮脏的黑客,我把中间件复制到COOKIE之前,会话中间件运行以恢复会话。
I've seen this done better with a decorator over the view, but this was the dirty hack I put in middleware to copy the POST into COOKIE before the session middleware runs to restore the session.
class FakeUploadCookieMiddleware(object):
"""TODO: replace the hardcoded url '/upload' with a 'reverse'."""
def process_request(self, request):
if request.path == '/upload/' \
and request.POST.has_key(settings.SESSION_COOKIE_NAME):
request.COOKIES[settings.SESSION_COOKIE_NAME] = \
request.POST[settings.SESSION_COOKIE_NAME]
logging.debug('Faking the session cookie for an upload: %s', \
request.POST[settings.SESSION_COOKIE_NAME])
这篇关于使用Django上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!