0

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!

R3dolaf
  • 5
  • 1
  • 4
  • See http://stackoverflow.com/questions/1156246/having-django-serve-downloadable-files – fasouto May 06 '14 at 16:46
  • already trayed, not working for me. I do not have access to the server (in order to configure it), i cannot install any kind of API or server or...nothing. I'm supoused to acomplish this task with the django 1.4 preinstaled utilities. (sorry for my bad englisht, not my native language...) – R3dolaf May 06 '14 at 19:23

2 Answers2

0

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

  • Anyway, I cannot even display the content of the txt. If you can point me in the right direction in order to do such thing, you'll me hero ^^ – R3dolaf May 06 '14 at 18:58
0

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

fasouto
  • 3,877
  • 2
  • 25
  • 60