這篇文章將為大家詳細講解有關(guān)使用Django怎么實現(xiàn)文件上傳下載功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
成都創(chuàng)新互聯(lián)主營烏爾禾網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,app軟件開發(fā)公司,烏爾禾h5小程序制作搭建,烏爾禾網(wǎng)站營銷推廣歡迎烏爾禾等地區(qū)企業(yè)咨詢創(chuàng)建項目:
django-admin startproject file_upload cd file_upload python manage.py startapp uploader
目錄結(jié)構(gòu):
0.settings.py
LANGUAGE_CODE = 'zh-Hans' TIME_ZONE = 'Asia/Shanghai' USE_I18N = True USE_L10N = True USE_TZ = False
1.urls.py
父:
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('uploader/', include('uploader.urls')) ]
子:
from django.contrib import admin from django.urls import path, include from . import views app_name = 'uploader' urlpatterns = [ path('', views.upload, name='upload'), # 上傳 path('list/', views.list), # 列表 path('download/', views.download, name='download'), # 下載 path('delete/ ', views.delete, name='delete'), # 刪除 ]
2.models.py
from django.db import models from django.utils import timezone ''' 文件記錄 ''' class FileInfo(models.Model): file_name = models.CharField(max_length=500) file_size = models.DecimalField(max_digits=10, decimal_places=0) file_path = models.CharField(max_length=500) upload_time = models.DateTimeField(default=timezone.now())
3.forms.py
from django import forms ''' 上傳表單 ''' class UploadForm(forms.Form): file = forms.FileField( widget=forms.ClearableFileInput(attrs={'multiple': True}), # 支持多文件上傳 label='選擇文件...', help_text='大100M' )
4.views.py
from django.shortcuts import render from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect from django.http import FileResponse from django.template import RequestContext from django.urls import reverse from django.utils.http import urlquote from .models import FileInfo from .forms import UploadForm import os # 上傳文件 def upload(request): # Handle file upload if request.method == 'POST': form = UploadForm(request.POST, request.FILES) if form.is_valid(): files = request.FILES.getlist('file') for f in files: file_info = FileInfo(file_name=f.name, file_size=1 if 0 < f.size < 1024 else f.size / 1024, file_path=os.path.join('D:\\upload', f.name)) file_info.save() # 上傳 destination = open(os.path.join("D:\\upload", f.name), 'wb+') for chunk in f.chunks(): destination.write(chunk) destination.close() # 返回上傳頁 return HttpResponseRedirect('/uploader/list') else: form = UploadForm() # A empty, unbound form return render(request, 'uploader/upload.html', {'form': form}) # 文件列表 def list(request): file_infos = FileInfo.objects.all() return render(request, 'uploader/list.html', {'file_infos': file_infos}) # 下載文件 def download(request, id): file_info = FileInfo.objects.get(id=id) print('下載的文件名:' + file_info.file_name) file = open(file_info.file_path, 'rb') response = FileResponse(file) response['Content-Disposition'] = 'attachment;filename="%s"' % urlquote(file_info.file_name) return response # 刪除文件 def delete(request, id): file_info = FileInfo.objects.get(id=id) file_info.delete() file_infos = FileInfo.objects.all() return HttpResponseRedirect('/uploader/list')
關(guān)于使用Django怎么實現(xiàn)文件上傳下載功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。