Skip to content

Instantly share code, notes, and snippets.

@zhasm
Created December 23, 2010 08:48
Show Gist options
  • Save zhasm/752752 to your computer and use it in GitHub Desktop.
Save zhasm/752752 to your computer and use it in GitHub Desktop.
uploading files within django framework
from django.conf.urls.defaults import *
from django.http import HttpResponse
'''
if required, please add the last 2 lines to settings.py:
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
# 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
'''
import base64
def hello(request):
html='''<html>
<body>
<form id="form" action='/upload' method="POST" enctype="multipart/form-data">
<input type="file" name="file" />
<input type='submit' value='Submit'>
</form>
</body>
</html>'''# % f.as_table()
return HttpResponse(html)
def upload(request):
if request.method == 'POST':
import StringIO
d=StringIO.StringIO()
file=request.FILES['file']
base64.encode(file, d)
return HttpResponse('''<img src="data:image/png;base64,%s">''' % d.getvalue())
else:
return HttpResponse("")
urlpatterns = patterns('',
(r"^$", hello),
(r"^upload$", upload),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment