随笔 - 493  文章 - 0  评论 - 97  阅读 - 239万

Antd Upload组件上传文件至php后端, php拿到对应的文件名

上传文件组件:UploadFile.jsx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import React from "react";
import * as antd from "antd";
 
const { Upload, Button, Icon, message } = antd;
 
class Wrapper extends React.Component {
  state = {};
 
  // 新打开界面
  componentDidMount() {}
 
  onChange = ({ file }) => {
    if (file.status === "done") {
      message.success(`${file.name} file uploaded successfully`);
      console.log(file);
      const {
        response: { code, msg, data }
      } = file;
 
      this.props.onOk({ code, msg, data });
    } else if (file.status === "error") {
      message.error(`${file.name} file upload failed.`);
    }
  };
 
  // 渲染
  render() {
    const { url } = this.props;
    const props = {
      name: "file",
      action: url,
      showUploadList: false,
      headers: {
        authorization: "authorization-text"
      },
      onChange: this.onChange
    };
 
    return (
      <Upload {...props}>
        <Button
          style={{ marginLeft: 20 }}
          onClick={() => {
            console.log("hhhh");
          }}
        >
          <Icon type="upload" />
          从文件导入
        </Button>
      </Upload>
    );
  }
}
 
export default Wrapper;

 组件使用:

1
<UploadFile url='/api/login/upload' onOk={this.onOk.bind(this)} />

 onOk 一般是放服务端处理完成后的后续操作!如:

1
2
3
onOk = res => {
        console.log('onOk', res);
    };

 

php 后端处理:

1
2
3
4
5
6
7
8
9
10
11
public function upload()
{
    $file = $_FILES['file'];
    $path = $file['tmp_name'];
 
    $data = ExcelModule::loadFile($path);   // 得到返回的数据
 
    log_message($data);
 
    return result(0, 'suc', $data);
}

 ExcelModule::loadFile是读取excel文件的内容,并返回array数据用的,实现如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static function loadFile(string $filePath)
    {
        try {
            $reader = \PHPExcel_IOFactory::createReaderForFile($filePath);
            $excel = $reader->load($filePath);
            $sheet = $excel->getActiveSheet();
 
            return $sheet->toArray();
        }
        catch(\Exception $e)
        {
            log_message(sprintf('读取excel文件失败: file=%s, errorMsg=%s', $filePath, $e->getMessage()));
        }
    }

 

posted on   清清飞扬  阅读(1015)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示