二进制文件下载响应的三种方式
This commit is contained in:
parent
a29a0137a9
commit
37dea519c1
@ -53,4 +53,7 @@ urlpatterns = [
|
|||||||
# 给路由分路由出来并命名
|
# 给路由分路由出来并命名
|
||||||
path('user/', include(('user.urls','user'), namespace='user')),
|
path('user/', include(('user.urls','user'), namespace='user')),
|
||||||
path('order/', include(('order.urls','order'), namespace='order')),
|
path('order/', include(('order.urls','order'), namespace='order')),
|
||||||
|
path('download1/', helloWorld.views.download_file1),
|
||||||
|
path('download2/', helloWorld.views.download_file2),
|
||||||
|
path('download3/', helloWorld.views.download_file3),
|
||||||
]
|
]
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
from django.http import HttpResponse
|
from django.http import HttpResponse, StreamingHttpResponse, FileResponse
|
||||||
from django.shortcuts import render, redirect
|
from django.shortcuts import render, redirect
|
||||||
|
|
||||||
|
|
||||||
@ -12,15 +12,48 @@ def index(request):
|
|||||||
|
|
||||||
|
|
||||||
def blog(request, id):
|
def blog(request, id):
|
||||||
|
# 输入http://localhost:8000/blog/0会跳转到http://localhost:8000/static/error.html
|
||||||
#输入http://localhost:8000/blog/0会跳转到http://localhost:8000/static/error.html
|
if id == 0:
|
||||||
if id==0:
|
|
||||||
return redirect("/static/error.html")
|
return redirect("/static/error.html")
|
||||||
else:
|
else:
|
||||||
return HttpResponse("id是" + str(id) + "的博客页面")
|
return HttpResponse("id是" + str(id) + "的博客页面")
|
||||||
|
|
||||||
|
|
||||||
def blog2(request, year, month, day, id):
|
def blog2(request, year, month, day, id):
|
||||||
return HttpResponse(str(year) + '/' + str(month) + '/' + str(day) + '/' + ' id是' + str(id) + "的博客页面")
|
return HttpResponse(str(year) + '/' + str(month) + '/' + str(day) + '/' + ' id是' + str(id) + "的博客页面")
|
||||||
|
|
||||||
|
|
||||||
def blog3(request, year, month, day):
|
def blog3(request, year, month, day):
|
||||||
return HttpResponse(str(year) + '/' + str(month) + '/' + str(day) + "的博客页面")
|
return HttpResponse(str(year) + '/' + str(month) + '/' + str(day) + "的博客页面")
|
||||||
|
|
||||||
|
|
||||||
|
# 定义文件路径
|
||||||
|
file_path = "D:\BaiduNetdiskDownload\Acrobat2024(64bit).zip"
|
||||||
|
|
||||||
|
|
||||||
|
# 第一个下载方法
|
||||||
|
def download_file1(request):
|
||||||
|
file = open(file_path, 'rb') # 打开文件
|
||||||
|
response = HttpResponse(file) # 创建HttpResponse对象
|
||||||
|
response['Content-Type'] = 'application/octet-stream'
|
||||||
|
response['Content-Disposition'] = 'attachment;filename=file1.zip'
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
# 第二个下载方法
|
||||||
|
|
||||||
|
def download_file2(request):
|
||||||
|
file = open(file_path, 'rb') # 打开文件
|
||||||
|
response = StreamingHttpResponse(file) # 创建StreamingHttpResponse对象
|
||||||
|
response['Content-Type'] = 'application/octet-stream'
|
||||||
|
response['Content-Disposition'] = 'attachment;filename=file2.zip'
|
||||||
|
return response
|
||||||
|
|
||||||
|
# 第三个下载方法
|
||||||
|
|
||||||
|
def download_file3(request):
|
||||||
|
file = open(file_path, 'rb') # 打开文件
|
||||||
|
response = FileResponse(file) # 创建FileResponse对象
|
||||||
|
response['Content-Type'] = 'application/octet-stream'
|
||||||
|
response['Content-Disposition'] = 'attachment;filename=file3.zip'
|
||||||
|
return response
|
||||||
|
|||||||
12
static/download.html
Normal file
12
static/download.html
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Title</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<a href="/download1/">下载测试一,HttpResponse</a><br>
|
||||||
|
<a href="/download2/">下载测试二,StreamingHttpResponse</a><br>
|
||||||
|
<a href="/download3/">下载测试三,FileResponse</a><br>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Loading…
Reference in New Issue
Block a user