前端代码
<el-upload
class="upload-demo"
ref="upload"
action="/api/v1/yw-sql" // 接口地址
multiple
:headers="importHeaders" // 添加请求头token
:on-preview="handlePreview" // 点击已上传的文件链接时的钩子, 可以通过 file.response 拿到服务端返回数据
:on-remove="handleRemove" // 删除时的钩子
:file-list="fileList"
:auto-upload="false"> // 是否自动上传
<el-button slot="trigger" size="small" type="primary">选取文件</el-button>
<el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
<div slot="tip" class="el-upload__tip">只能上传SQL文件,且不超过100M</div>
</el-upload>
后端代码
def post(self, request):
"""接收文件"""
try:
files = request.FILES.getlist("file",None)
for file in files:
sql_path = f"{os.getcwd()}/sql/{file.name}"
with open(sql_path, 'wb') as f:
for content in file.chunks():
print(content)
f.write(content)
return JSONApiResponse()
except Exception as e:
return JSONApiResponse(code=401, msg=str(e))