el-upload上传多个文件,一次请求,Django接收

1、:file-list="fileList" :on-change="handleChange" 将文件赋值到fileList

2、 :auto-upload="false" 手动触发上传 写个按钮点击执行这个 this.$refs.upload.submit();

3、自己写上传,不会再触发上传成功或失败回调

4、 request.FILES.getlist('file') 获取上传的多个文件,循环读取写入文件中

 <el-upload
     class="upload-demo"
     ref="upload"
     action=""
     :on-change="handleChange"
     :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">只能上传jmx/txt文件</div>
</el-upload>

<script>
    import axios from 'axios'
    export default{
        name:"StressTest",
            data() {
                     return {
                     fileList: []
                     }
            },
            components:{

            },
          
            methods:{
              handleChange(file, fileList) {
                console.log(file, fileList);
                this.fileList = fileList
                console.log('=====',this.fileList)
              },
              submitUpload() {  //上传
                    let formData = new FormData();  //  用FormData存放上传文件
                    this.fileList.forEach(file => {
                        formData.append('file', file.raw)
                    })
                    this.fileList = []
                    console.log('========',formData)
                    this.$axios.post('/uploadfile/', formData).then((res) => {
                        //手动上传无法触发成功或失败的钩子函数,因此这里手动调用
                      console.log('========','success')
                     
                       this.handleUploadSuccess()
                    }, (err) => {
                     
                       this.handleUploadError()
                    })
                },


               handleUploadSuccess(){
                this.$refs.upload.clearFiles()
                alert('上传成功')
               
                },
              handleUploadError(){
                 alert('上传失败')
             }

           // methods关闭
            }
    }
</script>

下面试着还是上次触发多次请求呢

<el-upload
    class="upload-demo"
    ref="upload"
    action=""
    :on-change="handleChange"
    :on-success="handleUploadSuccess"
    :on-error="handleUploadError"
    :file-list="fileList"
    :auto-upload="false"
    :http-request="uploadFile"
    >
    <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">只能上传jmx/txt文件</div>
</el-upload>


<script>
    import axios from 'axios'
    export default{
        name:"StressTest",
            data() {
                     return {
                     fileList: []
                    
                     }
            },
            components:{

            },
           
            methods:{
              submitUpload() {
                  this.$refs.upload.submit();
                },
              handleChange(file, fileList) {
                console.log(file, fileList);
                this.fileList = fileList
                console.log('=====',this.fileList)
              },
              uploadFile(file) {  //上传
                    let formData = new FormData();  //  用FormData存放上传文件
                    this.fileList.forEach(file => {
                        formData.append('file', file.raw)
                    })
                    console.log('========',formData)
                    this.$axios.post('/uploadurl/', formData).then((res) => {
                        //手动上传无法触发成功或失败的钩子函数,因此这里手动调用
                      console.log('========','success')
                       file.onSuccess(res)
                    }, (err) => {
                      file.onError(err)
                    })
              },


              handleUploadSuccess(response, file, fileList){
                this.$refs.upload.clearFiles()
                alert('上传成功')

              },
              handleUploadError(err, file, fileList){
                 alert('上传失败')
              }
         // methods关闭
         }
    }
</script>

def add_stressfile(request):
    data = {}
    for i in request.POST.lists():
        data[i[0]] = i[1][0]
    myFiles = request.FILES.getlist('file')
    print(myFiles)
    now = datetime.now()

    for myFile in myFiles:
        filename = str(now)[:-10].replace(' ', '_').replace(':', '') +'.txt'
        print(filename)
        file_path = os.path.join(settings.STRESS_ROOT, filename)
        fp = open(file_path, 'wb+')
        for i in myFile.chunks():
            # print(i)
            fp.write(i)
        fp.close()
       
    return HttpResponse(json.dumps({}), content_type='application/json')
相关推荐
STUPID MAN28 分钟前
vue3使用后端传递的文件流进行文件预览
前端·javascript·vue.js·文件预览
爬坑的小白37 分钟前
el-menu导航三级数据结构及数据展示
前端·javascript·vue.js
前端青山2 小时前
CSS 动画效果实现:图片展示与交互
开发语言·前端·javascript·css·vue.js·前端框架
工业互联网专业2 小时前
Python毕业设计选题:基于大数据的淘宝电子产品数据分析的设计与实现-django+spark+spider
大数据·python·数据分析·spark·django·课程设计·spider
Suppose7 小时前
[Vue]template相关
vue.js
cnsxjean8 小时前
Vue教程|搭建vue项目|Vue-CLI2.x 模板脚手架
javascript·vue.js·ui·前端框架·npm
MarisolHu8 小时前
前端学习笔记-Vue篇-02
前端·vue.js·笔记·学习
小周同学_丶9 小时前
解决el-select数据量过大的3种方法
前端·vue.js·elementui
花之亡灵10 小时前
(笔记)vue3引入Element-plus
前端·javascript·vue.js
以对_12 小时前
【el-table】表格后端排序
前端·javascript·vue.js