vue-antdesign文件上传,及vue调用扫码监听回车。

本文介绍了一种前端文件上传的实现方式,通过将流格式的文件传递给后端,获取图片地址,并将图片信息放入组件的fileList中。在上传过程中,使用FormData对象并结合axios发送POST请求。同时,文章还涉及监听扫码事件,动态新增表格行,并清空输入内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//方案:将流格式的文件传给后端,后端返回可访问的图片地址。再次查询时,按如下格式将后端给的图片放回到组件的filelist里。
//上传后查询图片,后端提供接口
   getProblemPhoto(this.problemResData.id).then((res) => {
        console.log(res);

        this.fileList = res.map((v) => ({
          uid: `${v.id}`,//随便写
          status: "done",//随便写
          name: v.fileName,// 重要
          url: v.imagePath,//最重要,filelist根据url识别图片
          resData: v,//备份,区分新老图片。
        }));
      });
// 文件上传,重要
export function tcalUploadFile (parameter) {
  const {fileList}=parameter;// fileList为组件onchange时的对象。

  const fd = new FormData()// 1.重要,new一个新文件格式对象
 fileList.forEach((file,i) => {
    fd.append(`files[${i}]`, file.originFileObj);//2.重要,后端需要的就是file里的originFileObj,组件change钩子可打印originFileObj。
  });
  return axios({
    url: `${url}`,
    method: 'post',
    params,
    data: fd,
    headers: {
      'Content-Type': 'application/json;charset=UTF-8',
      'Authorization':`${type} ${token}`//一般不需要。
    }
  
  });
}
//监听扫码,新增一行数据,清空扫出来的内容。扫描的内容会回写到input的value。
dom:<el-input v-model='sampleNo'></el-input>
    addScode() {
      const { count, tableSource } = this;

      const newData = {
        key: count,
        sampleNo: this.sampleNo,
      };
      this.tableSource[4].unshift(newData);
      this.count = count + 1;
      this.sampleNo='';
    },
  mounted() {
    document.onkeydown = (e) => {
      if (this.thisTable === 4 && e.keyCode === 13) {
        this.addScode();
      }
    };
  },
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值