From 37dea519c11e5e3fb140a2363872bf58ca2bebd0 Mon Sep 17 00:00:00 2001 From: mxjdi Date: Sun, 27 Apr 2025 22:55:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=8C=E8=BF=9B=E5=88=B6=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E5=93=8D=E5=BA=94=E7=9A=84=E4=B8=89=E7=A7=8D?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Django5/urls.py | 3 +++ helloWorld/views.py | 41 +++++++++++++++++++++++++++++++++++++---- static/download.html | 12 ++++++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 static/download.html 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 @@ + + + + + Title + + +下载测试一,HttpResponse
+下载测试二,StreamingHttpResponse
+下载测试三,FileResponse
+ + \ No newline at end of file