vue + django上传多个文件
vue
<el-upload
ref="upload"
:action=saveurl
enctype="multipart/form-data"
class="upload-demo"
:on-preview="handlePreview"
:on-remove="handleRemove"
:before-remove="beforeRemove"
:on-change='handleChange' // 文件状态改变时的钩子,添加文件、上传成功和上传失败时都会被调用
:auto-upload="false" // 选中后不自动上传
multiple
:limit="number"
:on-exceed="handleExceed"
:file-list="fileList">
<el-button size="small" type="primary">点击上传</el-button>
<div slot="tip" class="el-upload__tip">最多上传{{ number }}张图片</div>
</el-upload>
</div>
<!-- 按钮 -->
<div style="position: relative;top: 50px">
<el-button type="primary" style="margin-right: 200px" @click="save">提交</el-button>
<el-button @click="$router.go(-1)">取消</el-button>
</div>
<script>
export default {
data(){
return{
fileList: [],
}
},
methods: {
handleChange(file, fileList) {
this.fileList = fileList
},
},
beforeRemove(file, fileList) {
console.log(fileList)
return this.$confirm(`确定移除 ${file.name}?`).then(() => {
this.$message({
message: '删除成功',
type: 'success',
})
});
},
// 提交
save() {
let param = new FormData()
for (let i = 0; i < this.fileList.length; i++) {
param.append("images", this.fileList[i].raw);
}
param.append('name', this.name1) // 添加其他数据
this.$axios.post(this.$DjangoUrl + 'maintain/imgs/', param, {
'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryVCFSAonTuDbVCoAN', // 一定要带上请求头
}).then(res => {
})
},
}
django
class ImageSave(APIView):
def post(self, request):
print(request.data, request.FILES)
......
for i in request.FILES.getlist('images'): # 获取多个文件
print(i)
return APIResponse()