Antd Upload组件上传文件至php后端, php拿到对应的文件名(二: vue实现)


组件文件: UploadFile.vue






在Upload组件的change事件中监测file.status,控制外层Upload和Button的状态,防止上传过程中还能再次触发文件上传!

 组件调用者: index.vue






 后端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数据
	 * @param $filePath
	 * @return 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)
		{
			$msg = sprintf('读取excel文件失败: file=%s, errorMsg=%s', $filePath, $e->getMessage());
			log_message($msg);

			return $msg;
		}
	}