日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

详解django三种文件下载方式

發(fā)布時間:2025/3/16 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 详解django三种文件下载方式 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

推薦使用FileResponse,從源碼中可以看出FileResponse是StreamingHttpResponse的子類,內(nèi)部使用迭代器進行數(shù)據(jù)流傳輸。

在實際的項目中很多時候需要用到下載功能,如導excel、pdf或者文件下載,當然你可以使用web服務自己搭建可以用于下載的資源服務器, 如nginx,這里我們主要介紹django中的文件下載。 實現(xiàn)方式:a標簽+響應頭信息(當然你可以選擇form實現(xiàn)) <div class="col-md-4"><a href="{% url 'download' %}" rel="external nofollow" >點我下載</a></div> 方式一:使用HttpResponse 路由url:url(r'^download/',views.download,name="download"), views.py代碼from django.shortcuts import HttpResponsedef download(request):file = open('crm/models.py', 'rb')response = HttpResponse(file)response['Content-Type'] = 'application/octet-stream' #設置頭信息,告訴瀏覽器這是個文件response['Content-Disposition'] = 'attachment;filename="models.py"'return response 方式二:使用StreamingHttpResponse, 其他邏輯不變,主要變化在后端處理:from django.http import StreamingHttpResponsedef download(request):file=open('crm/models.py','rb')response =StreamingHttpResponse(file)response['Content-Type']='application/octet-stream'response['Content-Disposition']='attachment;filename="models.py"'return response 方式三:使用FileResponse from django.http import FileResponse def download(request):file=open('crm/models.py','rb')response =FileResponse(file)response['Content-Type']='application/octet-stream'response['Content-Disposition']='attachment;filename="models.py"'return response

轉(zhuǎn)載于:https://www.cnblogs.com/nyist-xsk/p/10155509.html

總結

以上是生活随笔為你收集整理的详解django三种文件下载方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。