【VUE+FLASK学习】Vue使用element-ui上传文件至服务器端

3 篇文章 0 订阅

目标:将数据上传至服务器指定文件夹中,同时可以获取返回值

实现工具:element-ui —》el-upload、flask

Vue页面

核心参数(action:参数为目标地址,该处为 服务器地址/flask路由地址

其余参数可以寻找element-ui文档进行对照。

      <el-upload
        class="upload-demo"
        ref="upload"
        action="http://127.0.0.1:5000/xxx"
        :file-list="fileList"
        :auto-upload="false"
        :on-success="handleSuccess"
      >
        <el-button slot="trigger" size="small" type="primary"
          >选取文件</el-button
        >
        <el-button
          style="margin-left: 10px"
          size="small"
          type="success"
          @click="submitUpload"
          >上传服务端</el-button
        >
      </el-upload>

 FLASK代码

outinfo为获取的返回值,可根据自己的需要进行处理。

# 数据上传处理
@app.route('/xxx',methods=['POST'])
@cross_origin()  # 跨域注解
def UpGeojsonFile():
    if request.method == 'POST':
        f = request.files['file']
        filename = secure_filename(f.filename)
        # print(filename)
        # 验证文件格式
        types = ['jpg','png']
        if filename.split('.')[-1] in types:
            # 获取上传文件的服务器存储地址
            file_path = os.path.join(file_dir,'{0}'.format(filename))
            # print(file_path)
            # 文件存储
            f.save(file_path)
        outinfo = filename + '上传成功'
        return outinfo
    else:
        return 'error'

 Vue中 返回值调用函数             

handleSuccess() 已在<el-upload>中:on-success 标注 

methods:{
    handleSuccess(res, file, fileList) {
          console.log(res)
        },
}

结果:

 

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值