Найти - Пользователи
Полная версия: Скачиванеи файлов.
Начало » Django » Скачиванеи файлов.
1
Alex_Kutsan
('^data/(.+)$', files),
Что должна возвращать функция files чтоб при переходе по ссылке скачивать фаил?
kachayev
HttpResponse должна вернуть, как и обычно.
Только нужно правильно указать mimetype и добавить заголовок Content-Disposition. Например,

response = HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename=my.pdf'
## ...
return response
Alex_Kutsan
А если я не знаю точно какой тип файла?
И где должен хранится это т самый my.pdf
slav0nic
def show_attachment(request, hash):
attachment = get_object_or_404(Attachment, hash=hash)
file_data = file(attachment.get_absolute_path(), 'rb').read()
response = HttpResponse(file_data, mimetype=attachment.content_type)
response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(attachment.name)
return response
я так делал (как сейвится можешь глянуть на https://bitbucket.org/slav0nic/djangobb/src/4d897972bfeb/djangobb/djangobb_forum/forms.py#cl-51)
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB