开发者

Return a downloadable file link given a string with Django

开发者 https://www.devze.com 2023-04-10 06:54 出处:网络
Given a URL string which points to a file on my local nginx instance, how do I ma开发者_如何学编程ke Django return a response that causes the file to be downloaded from nginx?I don\'t want Django to b

Given a URL string which points to a file on my local nginx instance, how do I ma开发者_如何学编程ke Django return a response that causes the file to be downloaded from nginx? I don't want Django to be opening up the file and streaming it.


Use the X-Accel-Redirect header: http://wiki.nginx.org/XSendfile

For example:

resp = HttpResponse()
resp['X-Accel-Redirect'] = "/static/my_file"
return resp

Note that, as per the documentation, you'll need to translate the local path into a URL which nginx can serve.

0

精彩评论

暂无评论...
验证码 换一张
取 消