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')
相关推荐
ElasticPDF-新国产PDF编辑器15 小时前
Vue use pdf.js and Elasticpdf tutorial
vue.js·pdf
强化生物科研小助手16 小时前
CAS:47623-98-3,DiSBAC2(3)一种慢反应的膜电位敏感探针
django·html·pygame
Billy Qin16 小时前
Tree - Shaking
前端·javascript·vue.js
月明长歌17 小时前
Vue + Axios + Mock.js 全链路实操:从封装到数据模拟的深度解析
前端·javascript·vue.js·elementui·es6
头顶秃成一缕光17 小时前
若依——基于AI+若依框架的实战项目(实战篇(下))
java·前端·vue.js·elementui·aigc
冴羽yayujs17 小时前
SvelteKit 最新中文文档教程(17)—— 仅服务端模块和快照
前端·javascript·vue.js·前端框架·react
海石17 小时前
vue2升级vue3踩坑——【依赖注入】可能成功了,但【依赖注入】成功了不太可能
前端·vue.js·响应式设计
橘猫云计算机设计18 小时前
基于springboot的考研成绩查询系统(源码+lw+部署文档+讲解),源码可白嫖!
java·spring boot·后端·python·考研·django·毕业设计
cypking19 小时前
解决 axios get请求瞎转义问题
vue.js
向阳25619 小时前
SpringBoot+vue前后端分离整合sa-token(无cookie登录态 & 详细的登录流程)
java·vue.js·spring boot·后端·sa-token·springboot·登录流程