Last active
August 29, 2015 13:58
-
-
Save amitittyerah/10381708 to your computer and use it in GitHub Desktop.
Simple file upload example without forms for Django
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# view.py | |
def upload(request): | |
if 'file' in request.FILES: | |
upload_location = ModelName.upload_file(request.FILES.get('file')) | |
if upload_location: | |
print upload_location | |
else: | |
print 'oops, file upload failed!' | |
# models.py | |
from app.settings import MEDIA_ROOT | |
class ModelName(models.Model): | |
@staticmethod | |
def upload_file(file_instance): | |
file_name = file_instance.name | |
file_path = '%s/%s' % (MEDIA_ROOT, file_name) | |
destination = open(file_path, 'w') | |
if file_instance.multiple_chunks(): | |
for chunk in file_instance.chunks(): | |
destination.write(chunk) | |
else: | |
destination.write(file_instance.read()) | |
destination.close() | |
return file_path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment