Domanda

Ho una vista Django che restituisce un HttpResponse con un tipo MIME speciale per rendere il browser dell'utente " download " il file invece di visualizzarlo nel browser. Il problema è che il nome file predefinito a cui verrà salvata la risposta è l'URL a cui l'utente ha tentato di accedere.

Esiste un modo per includere un nome file predefinito nell'oggetto HttpResponse o devo reindirizzare a un URL diverso per farlo accadere?

È stato utile?

Soluzione

C'è un esempio pertinente nei docs :

from django.http import HttpResponse

def some_view(request):
    # Create the HttpResponse object with the appropriate headers.
    response = HttpResponse(mimetype='application/pdf', <snip>)
    response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'
    return response
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top