Загрузка файла на сервер
- Автор: Silver
- Просмотров: 458
Шаблон:
<form id="fm_upload" class="upload_form" action="/messages/upload" method="post" enctype="multipart/form-data"> <label for="upl_file">Отправить файл: </label><input id="file" type="file" name="upl_file" /> </form>Вьюха:
def upload_file_view(request): if not request.user.is_authenticated(): return render_to_response('index.html') if request.method == 'POST': upl_file=request.FILES['upl_file'] if upl_file and upl_file.size < 10485760: import os upl_root=os.getcwd()+UPLOAD_DIR if not os.path.exists(upl_root+request.user.username+'/'): os.mkdir(upl_root+request.user.username+'/') with open(upl_root+request.user.username+'/'+upl_file.name,'wb') as fh: for chunk in upl_file.chunks(): fh.write(chunk)