一、效果展示
文件上传和展示:
文件搜索:
文件下载:
删除文件:
二、关键代码
#urls.py
from django.urls import path,re_path from .views import HomeView,DisplayView,MyView,SearchView from . import views app_name = \'share\' urlpatterns = [ path(\'\', HomeView.as_view(), name=\'home\'), # 当用户发起主页的GET请求时 # 会调用 HomeView的父类的get方法处理 # 怎么调用呢,这里需要用到HomeView的父类的as_view方法 # 此方法会调用dispatch方法, 由后者根据请求类型选择响应的处理函数 path(\'s/<code>\', DisplayView.as_view(), name=\'display\'), #展示上传成功的文件 path(\'my/\', MyView.as_view(), name=\'my\'), #管理文件 path(\'search/\', SearchView.as_view(), name=\'search\'), #搜索文件 re_path(r\'^download/(?P<id>\\d*$)\', views.Download, name=\'download\'), #下载文件 re_path(r\'^delete/(?P<id>\\d*$)\', views.DeleteFile, name=\'delete\'), #删除文件 ]
#views.py
from django.shortcuts import render from django.views.generic import TemplateView,ListView import random import string import json from django.http import HttpResponse,HttpResponsePermanentRedirect,StreamingHttpResponse,HttpResponseRedirect from .models import Upload import os # 创建视图类需要基础视图基类 # 例如 TemplateView 就是用于展示页面的模板视图基类 # 该类仅提供get方法,用于处理GET请求 # 当然也可以自定义其他方法,例如post # 展示主页的话,只需要提供模板文件的名字即可 # 当客户端发起get请求时,由父类的get方法处理请求 # 该属性用于提供模板文件,在父类的方法中被调用 class HomeView(TemplateView): \'\'\'用来展示主页的视图类 \'\'\' template_name = \"base.html\" def post(self, request): if request.FILES: #如果表单中有文件 file = request.FILES.get(\'file\') code = \'\'.join(random.sample(string.digits, 8)) #设置文件的唯一标识code,并且用作文件改名 name = file.name size = int(file.size) path = \'filesystem/static/file/\' + code + name #设置文件保存的路径,并且更改文件名(防止重名文件) with open(path, \'wb\') as f: f.write(file.read()) #保存文件 upload = Upload( path = path, name = name, filesize = size, code = code, pcip = str(request.META[\'REMOTE_ADDR\']) ) upload.save() #向数据库插入数据 return HttpResponsePermanentRedirect(\"/share/s/\"+code) #重定向到DisplayView class DisplayView(ListView): \'\'\'展示文件的视图类 \'\'\' def get(self, request, code): uploads = Upload.objects.filter(code=code) #显示出指定文件 if uploads: for upload in uploads: upload.dowmloadcount += 1 #记录访问次数 upload.save() return render(request, \'content.html\', {\'content\':uploads, \'host\':request.get_host()}) class MyView(ListView): \'\'\' 用户管理视图类,就是用户管理文件的那个页面的视图类 \'\'\' def get(self, request): #ip = request.META[\'REMOTE_ADDR\'] #uploads = Upload.objects.filter(pcip=ip) uploads = Upload.objects.all() #查询所有文件 for upload in uploads: upload.dowmloadcount += 1 upload.save() return render(request, \'content.html\', {\'content\':uploads}) #将所有文件信息渲染展示 class SearchView(ListView): \'\'\'搜索功能的视图类 \'\'\' def get(self, request): code = request.GET.get(\'kw\') # 获取关键词 u = Upload.objects.filter(name__icontains=str(code)) # 模糊搜索 # select * from share_upload where name like \'%code%\'; data = {} if u: # 将符合条件的数据放到data中 for i in range(len(u)): # 循环输出查询的结果 u[i].dowmloadcount += 1 u[i].save() data[i]={} data[i][\'download\'] = u[i].dowmloadcount data[i][\'filename\'] = u[i].name data[i][\'id\'] = u[i].id data[i][\'ip\'] = str(u[i].pcip) data[i][\'size\'] = u[i].filesize data[i][\'time\'] = str(u[i].datetime.strftime(\'%Y-%m-%d %H:%M\')) return HttpResponse(json.dumps(data), content_type=\"application/json\") def Download(request, id): \"\"\" 下载压缩文件 :param request: :param id: 数据库id :return: \"\"\" data = Upload.objects.all() file_name = \"\" # 文件名 for i in data: if i.code == id: # 判断id一致时 file_name = i.name # 覆盖变量 base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # 项目根目录 file_path = os.path.join(base_dir, \'filesystem\', \'static\', \'file\', file_name) # 下载文件的绝对路径 if not os.path.isfile(file_path): # 判断下载文件是否存在 return HttpResponse(\"Sorry but Not Found the File\") def file_iterator(file_path, chunk_size=512): \"\"\" 文件生成器,防止文件过大,导致内存溢出 :param file_path: 文件绝对路径 :param chunk_size: 块大小 :return: 生成器 \"\"\" with open(file_path, mode=\'rb\') as f: while True: c = f.read(chunk_size) if c: yield c else: break try: # 设置响应头 # StreamingHttpResponse将文件内容进行流式传输,数据量大可以用这个方法 response = StreamingHttpResponse(file_iterator(file_path)) # 以流的形式下载文件,这样可以实现任意格式的文件下载 response[\'Content-Type\'] = \'application/octet-stream\' # Content-Disposition就是当用户想把请求所得的内容存为一个文件的时候提供一个默认的文件名 response[\'Content-Disposition\'] = \'attachment;filename=\"{}\"\'.format(file_name) except: return HttpResponse(\"Sorry but Not Found the File\") return response def DeleteFile(request, id): \'\'\'删除指定文件\'\'\' data = Upload.objects.all() file_name = \"\" # 文件名 for i in data: if i.code == id: # 判断id一致时 file_name = i.code + i.name # 覆盖变量 base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # 项目根目录 file_path = os.path.join(base_dir, \'filesystem\', \'static\', \'file\', file_name) # 下载文件的绝对路径 if os.path.exists(file_path): os.remove(file_path) file = Upload.objects.get(code=id) file.delete() return HttpResponseRedirect(\'/share/my/\') \'\'\' except Exception as e: return http.HttpResponseForbidden(\'文件不存在,下载失败!\') \'\'\'
© 版权声明
THE END
暂无评论内容