diff --git a/Django5/urls.py b/Django5/urls.py index f2cf339..e0d8180 100644 --- a/Django5/urls.py +++ b/Django5/urls.py @@ -53,4 +53,7 @@ urlpatterns = [ # 给路由分路由出来并命名 path('user/', include(('user.urls','user'), namespace='user')), 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), ] diff --git a/helloWorld/views.py b/helloWorld/views.py index ed5ec90..eb67453 100644 --- a/helloWorld/views.py +++ b/helloWorld/views.py @@ -1,4 +1,4 @@ -from django.http import HttpResponse +from django.http import HttpResponse, StreamingHttpResponse, FileResponse from django.shortcuts import render, redirect @@ -12,15 +12,48 @@ def index(request): def blog(request, id): - - #输入http://localhost:8000/blog/0会跳转到http://localhost:8000/static/error.html - if id==0: + # 输入http://localhost:8000/blog/0会跳转到http://localhost:8000/static/error.html + if id == 0: return redirect("/static/error.html") else: return HttpResponse("id是" + str(id) + "的博客页面") + def blog2(request, year, month, day, id): return HttpResponse(str(year) + '/' + str(month) + '/' + str(day) + '/' + ' id是' + str(id) + "的博客页面") + def blog3(request, year, month, 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 diff --git a/static/download.html b/static/download.html new file mode 100644 index 0000000..04c97bb --- /dev/null +++ b/static/download.html @@ -0,0 +1,12 @@ + + +
+ +