문제

This is my model

class Informe(models.Model):
id_paciente = models.ForeignKey('Paciente')
id_medico = models.ForeignKey('Medico')
id_tecnico = models.ForeignKey('Tecnico')
contenido = models.FileField(upload_to='informes', verbose_name='informe')
def relative_path(self):
    os.path.realpath(self.path, settings.MEDIA_ROOT)

I'm supoussed to download the txt file saved in /files/informes. I can get without problems the url or the path in the template, but have no clue about how to being able to download it. How can I manage to, whenever the client clicks on the template's link, autodownload the file?

TIA!

도움이 되었습니까?

해결책

I found this in the Django documentation:

Telling the browser to treat the response as a file attachment

To tell the browser to treat the response as a file attachment, use the content_type argument and set the Content-Disposition header. For example, this is how you might return a Microsoft Excel spreadsheet:

response = HttpResponse(my_data, content_type='application/vnd.ms-excel') response['Content-Disposition'] = 'attachment; filename="foo.xls"'

https://docs.djangoproject.com/en/dev/ref/request-response/#telling-the-browser-to-treat-the-response-as-a-file-attachment

다른 팁

You cannot force the users browser to auto-download txt files. Most browsers will open the txt file for viewing in the browser. The client would be able to change browser settings to download txt files instead of viewing in the browser.

Here is a more in depth post on this :

http://www.htmlhelp.com/faq/html/links.html#force-download

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top