el-upload上传文件携带额外参数

在进行文件上传时,需要传递其他参数,比如下图中需要实现携带下拉框的参数

前端实现:将下拉框中的参数 传递到:data中 

:data="{'script_model':script_model}"  

<el-dialog title="上传脚本" :visible.sync="up_script_visible" style="line-height:18px;width: 100%;">
      <el-select v-model="script_model" style="float: left">
        <el-option label="other脚本(低性能)" value="other"></el-option>
        <el-option label="python脚本(中性能)" value="python"></el-option>
        <el-option label="go脚本(高性能)" value="go"></el-option>
      </el-select>
      <br><br><br>
      <el-upload
          :data="{'script_model':script_model}"  
          style="float: left"
          :action="get_action()"
          :limit="1"
          name="script_file"
    >
        <el-button size="mini" type="primary">上传脚本</el-button>
      </el-upload>
      <br><br><br><br>
    </el-dialog>


  methods:{
    get_action(){
      return process.env.VUE_APP_BASE_URL+'/upload_script_file/'
    },
}

 后端实现:

从post请求中获取携带的参数:

 script_model = request.POST.get('script_model')

# 上传文件
def upload_script_file(request):
    script_model = request.POST.get('script_model')
    myFile = request.FILES.get('script_file')
    file_name = str(myFile)
    fp = open('scripts/' + script_model + '/' + file_name, 'wb+')
    for i in myFile.chunks():
        fp.write(i)
    fp.close()
    return HttpResponse('')

猜你喜欢

转载自blog.csdn.net/weixin_46361114/article/details/129916362