有没有办法使用Django Response从远程URL流式传输文件(不在本地下载文件)?
gfortran -std=legacy -o grdsp96 grdsp96.f
grdsp96.f:1872.72:
open (1,file=dfile(i),access='append',form='print')
1
Error: FORM specifier in OPEN statement at (1) has invalid value 'print'
grdsp96.f:2052.72:
open (1,file=dfile(i),status='unknown',form='print')
1
Error: FORM specifier in OPEN statement at (1) has invalid value 'print'
make: *** [all] Error 1
我们有文件存储(文件可能很大 - 1 GB甚至更多)。我们无法共享下载URL(存在安全问题)。文件流可以显着 通过将下载流转发到Django响应来提高下载速度。
答案 0 :(得分:5)
Django内置了StreamingHttpResponse类,应该给出一个迭代器,它将字符串作为内容。在下面的示例中,我使用的是requests
Raw Response Content
import requests
from django.http import StreamingHttpResponse
def strem_file(request, *args, **kwargs):
r = requests.get("http://host.com/file.txt", stream=True)
resp = StreamingHttpResponse(streaming_content=r.raw)
# In case you want to force file download in a browser
# resp['Content-Disposition'] = 'attachment; filename="saving-file-name.txt"'
return resp