0

I want to be able to click a button that triggers a download, for a better understanding, my codes are below

    import mimetypes

    From wsgiref.util import Filewrapper

    def audio_download (request, pk):
        download =get_object_or_404(Audio,pk=pk)

        file =download.audio.audio.url.strip('/')
        wrapper = FileWrapper(open(file, 'rb'))
        response= HttpResponse(wrapper, content_type='application/force-download')
        response ['Content-Disposition]="attachment; filename="+os.path.basename(file)
        Print ('response')
        return response

Then my url:

  url (r'^audio/download/(P<pk>\d+)/$, views.audio_download, name= 'audio_download')

Then lastly, HTML

    {%for audio in audio%}
    <a href = "audio/download/{{audio.id}}">{{audio.title}}</a></center>
    {%endfor%}
kc nweke
  • 1
  • 4
  • 1
    I think your question was already answered for example here: https://stackoverflow.com/a/36394206/2047157 – darkless Mar 31 '20 at 07:14
  • Inside the function `audio_download` use debugger like `ipdb` to check what is going on. – Kishan Mehta Mar 31 '20 at 07:16
  • very probably unrelated, but you should not hardcode your url (nor use relative url path FWIW) - [use the `{% url %}` templatetag instead](https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#url) – bruno desthuilliers Mar 31 '20 at 07:24
  • @darkless, thanks, but there's no pk (primary key) used and what does file_path really means in the code.. thanks – kc nweke Mar 31 '20 at 07:38
  • you need to get `file_path` (as in the link) from the `download` object you got, which I guess could be download.audio.path. See: https://docs.djangoproject.com/en/3.0/topics/files/#using-files-in-models – darkless Mar 31 '20 at 07:47
  • @darkless, thanks for the link which was really helpful, I followed the thread in the Link and landed at Page not found (404)... Thanks – kc nweke Mar 31 '20 at 12:07
  • 1
    @darkless am really grateful, files like PDF, doc works fine with the reference Link you shared with me, but file like mp4 videos doesn't seem to download.. please help – kc nweke Apr 01 '20 at 21:36

2 Answers2

0

I think instead of HttpResponse:

response= HttpResponse(wrapper, content_type='application/force-download')

you can try using FileResponse:

from django.http import FileResponse

response= FileResponse(open(file, 'rb'), content_type='application/force-download')
response ['Content-Disposition]="attachment; filename="+os.path.basename(file)
Charnel
  • 4,222
  • 2
  • 16
  • 28
0

I got an answer to my question, I felt I should share with other users who have similar issues

import mimetypes
From wsgiref.util import Filewrapper


def audio_download (request, path):
    audio_path =os.path.join(settings.MEDIA_ROOT, path)
if os.path.exists(file_path):
    with open(file_path, 'rb') as fh:
    response= HttpResponse(fh.read(), content_type='application/mp3')
    response ['Content-Disposition]="inline; filename="+os.path.basename(file_path)
    Print ('response')
    return response
raise Http404

The remaining of my template and URLs remains the same

kc nweke
  • 1
  • 4