

Django(drf)配合 Vue Element 实现文件上传下载功能
source link: http://www.starky.ltd/2021/03/13/django-drf-and-vue-element-file-upload-and-download/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Django(drf)配合 Vue Element 实现文件上传下载功能
Models
编辑 models.py
代码,通过 FileField
字段记录文件信息:
from django.db import models
class FilesModel(models.Model):
file = models.FileField(upload_to='uploads/')
class Meta:
db_table = 'files_storage'
ordering = ['-id']
Serializer
这里使用 Django REST framework 实现后端 REST API,需要创建序列化器 serializers.py
,内容如下:
from rest_framework import serializers
# files 是 app 的名字
from files import models
class FilesSerializer(serializers.ModelSerializer):
class Meta:
model = models.FilesModel
fields = '__all__'
Views
编辑 views.py
代码,内容如下:
from rest_framework.viewsets import ModelViewSet
from files import models, serializers
class FileViewSet(ModelViewSet):
queryset = models.FilesModel.objects.all()
serializer_class = serializers.FilesSerializer
在 files 路径下新建 urls.py
文件,填写路由配置:
from django.urls import include, path
from rest_framework import routers
from files import views
router = routers.DefaultRouter()
router.register(r'files', views.FileViewSet)
urlpatterns = [
path('', include(router.urls))
]
在项目总配置路径下(settings.py
所在的路径)编辑根路由配置文件 urls.py
:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('storage/', include('files.urls'))
]
测试后端 API
运行后台服务 python manage.py runserver 0.0.0.0:8000
,访问 http://xx.xx.xx.xx:8000/storage/files/,界面如下:
测试上传文件,效果如下:
前端代码(手动上传)
借助 Element UI 的 upload 组件,Vue 代码(index.vue
)如下:
<template>
<div>
<el-upload
ref="upload"
drag
action="http://xx.xx.xx.xx:8000/storage/files/"
:auto-upload="false"
:on-success="onSuccess"
>
<i class="el-icon-upload" />
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
</el-upload>
<el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
</div>
</template>
<script>
export default {
name: 'UploadDemo',
methods: {
submitUpload() {
this.$refs.upload.submit()
},
onSuccess() {
this.$message.success('上传成功')
}
}
}
</script>
其中 el-upload
组件的 action
属性用于指定后台 API 的 URI;:auto-upload
属性用于设置是否自动上传(这里设置为 false
,手动触发上传动作);:on-success
属性用于指定上传成功后触发的方法。
submitUpload()
中的 this.$refs.upload.submit()
方法触发文件上传动作。
界面如下:
测试文件上传:
后台数据如下:
[
{
"file": "http://172.20.23.34:8000/storage/files/uploads/template.html",
"id": 18
},
{
"file": "http://172.20.23.34:8000/storage/files/uploads/20171215091830_55126_hSnPtZR.png",
"id": 17
}
]
文件上传的同时添加其他数据
修改数据库模型
编辑后端 models.py
文件,添加其他字段:
from django.db import models
class FilesModel(models.Model):
name = models.CharField(max_length=20, default='')
file = models.FileField(upload_to='uploads/')
class Meta:
db_table = 'files_storage'
ordering = ['-id']
数据库迁移后,重启后台 Web 服务。
后台数据如下:
[
{
"file": "http://172.20.23.34:8000/storage/files/uploads/template.html",
"id": 18,
"name": ""
},
{
"file": "http://172.20.23.34:8000/storage/files/uploads/20171215091830_55126_hSnPtZR.png",
"id": 17,
"name": ""
}
]
修改前端代码
添加其他数据的输入界面,同时将附加数据绑定到 el-upload
组件中:
<template>
<div>
<el-label>名称</el-label>
<el-input v-model="fileData.name" style="width: 20%" />
<el-upload
ref="upload"
drag
class="upload-demo"
action="http://xx.xx.xx.xx:8000/storage/files/"
:data="fileData"
:auto-upload="false"
:on-success="onSuccess"
style="padding: 30px"
>
<i class="el-icon-upload" />
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
</el-upload>
<el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
</div>
</template>
<script>
export default {
name: 'UploadDemo',
data() {
return {
fileData: {
name: ''
}
}
},
methods: {
submitUpload() {
this.$refs.upload.submit()
},
onSuccess() {
this.$message.success('上传成功')
}
}
}
</script>
其中 el-upload
组件的 :data
属性用于指定文件上传时附加的数据(类型为 JavaScript 对象)。
文件上传测试:
上传完成,后台数据如下:
[
{
"file": "http://172.20.23.34:8000/storage/files/uploads/AnyDesk.exe",
"id": 19,
"name": "测试文件"
},
{
"file": "http://172.20.23.34:8000/storage/files/uploads/template.html",
"id": 18,
"name": ""
},
{
"file": "http://172.20.23.34:8000/storage/files/uploads/20171215091830_55126_hSnPtZR.png",
"id": 17,
"name": ""
}
]
修改后台视图代码(views.py
),添加文件下载的 API 响应逻辑:
from rest_framework.viewsets import ModelViewSet
from files import models, serializers
from rest_framework.decorators import action
from django.http import FileResponse
class FileViewSet(ModelViewSet):
queryset = models.FilesModel.objects.all()
serializer_class = serializers.FilesSerializer
@action(methods=['get', 'post'], detail=True)
def download(self, request, pk=None, *args, **kwargs):
file_obj = self.get_object()
response = FileResponse(open(file_obj.file.path, 'rb'))
return response
此时访问 http://xx.xx.xx.xx:8000/storage/files/[id]/download/ 链接,即可直接下载上传到服务器上的文件。
$ curl -o anydesk.exe 172.20.23.34:8000/storage/files/19/download/
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 3584k 100 3584k 0 0 102M 0 --:--:-- --:--:-- --:--:-- 102M
Recommend
-
118
一、Windows主机自动从Linuxftp服务器下载文件二、Linux主机自动从Windowsftp服务器下载文件
-
56
-
39
介绍 Docker 是一种面向应用开发和运维人员的开发、部署和运行的容器平台,相对于 Virtual Machine 更加轻量,底层使用 Linux Namespace(UTS、IPC、PID、Network、Mount、User)和 cgroups(Control Groups)技术对应用进程进...
-
56
README.md wps store micro service protoc-gen mv protoc-gen-swagger /usr/local/go/bin mv protoc-gen-grpc-gateway /usr/local/go/bin 编译...
-
31
byzhangxinxu from https://www.zhangxinxu.com/wordpress/?p=8598 本文可全文转载,个人网站无需授权,但需要保留原作者、出处以及文中链接,任何网站均可摘要聚...
-
50
paramiko模块 paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接。 因此,如果需要使用SSH从一个平台连接到另外一个平台,进行一系列的操作时,paramiko是最佳工具之一...
-
27
1,什么是大数据? 什么是大数据?先来一组直观的数据感受下什么是大数据: 1)纽约证交所每天产生的交易数据大约是4TB到5TB之间; 2)截止到2015年7月,微信朋友圈每天的发表量(包括赞和评论)超过10亿,...
-
10
WebSSH画龙点睛之lrzsz上传下载文件 本篇文章没有太多的源码,主要讲一下实现思路和技术原理 当使用Xshell或者SecureCRT终端工具时,我的所有文件传输工作都是通过lrzsz来完成的,主要是因为...
-
4
django文件上传功能 2013-12-04编程技术 今天需要一个文件上传功能,所以代码如下
-
18
HTTP 上传下载实现 @POST· 2023-08-20 08:33 · 69 min read 在互联网中,上传和下载都是非常常见的操作,而且不同于内网环境,通常这些操作都是通过 HTTP 协议实现的,所以在这篇文章中我尝试解释一下 HTTP...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK