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

上传文件组件:UploadFile.jsx

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 (

style={{ marginLeft: 20 }}

onClick={() => {

console.log("hhhh");

}}

>

从文件导入

);

}

}

export default Wrapper;

组件使用:

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

onOk = res => {

console.log('onOk', res);

};

php 后端处理:

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数据用的,实现如下:

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()));

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值