我必须在MySql表中插入excel文件的数据,可以从一个位置选择,我正在使用Symfony,PHPExcel和Ajax。我在表格中创建了一个文件上传按钮。我点击选择文件,选择文件,然后点击上传文件然后我在控制台上收到错误,如无法打开“C:\ fakepath \ CRS_Data.xls进行读取!文件不存在”。我在这里读过这么多相同的问题,但是即使看完这些问题我也无法解决问题。以下是我的代码。所以请有人建议解决问题。
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$('#uploadFile').click(function(e){
var file_path = document.getElementById("file").value;
e.preventDefault();
var urlAjax = "<?php echo URL_AJAX . '/birthPublicSearch' ?>";
// birthPublicSearch is the module name and UploadFile is action name
$.ajax({
url: urlAjax + "/UploadFile",
data: {
file_path: file_path
},
async: false,
type: "POST",
success: function(response){
console.log(response);
alert("Data Uploaded Successful");
}
});
});
});
</script>
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<td bgcolor="#E6E6FA"><font color="red">Upload File</font></td>
<td></td>
<td bgcolor="#E6E6FA">
<input type="file" name="file" id="file">
</td>
</tr>
<tr>
<td colspan="2" align="center" style="padding-bottom: 10px;">
<input type="button" id="uploadFile" name="uploadFile" value="Upload File" class="btn"></input>
</td>
</tr>
</table>
行动定义为:
public function executeUploadFile(sfWebRequest $request) {
require_once('/../Classes/PHPExcel.php');
require_once '/../Classes/PHPExcel/IOFactory.php';
$path=$_REQUEST['file_path'];
// path I get is as C:\fakepath\CRS_Data.xls
$objPHPExcel = PHPExcel_IOFactory::load($path);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet){
$worksheetTitle = $worksheet->getTitle();
$highestRow = $worksheet->getHighestRow();
$highestColumn = $worksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
}
//Inserting datas into db
$i=1;
$j=0;
for ($row = 1; $row <= $highestRow; ++ $row)
{
$val=array();
for ($col = 0; $col < $highestColumnIndex; ++ $col)
{
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$val[] = $cell->getValue();
}
// Insert data into mysql
$sql="INSERT INTO tempbackuptable(`bir_le_id`,`bir_le_form_no`,)VALUES($val[0], $val[1])";
$result=mysql_query($sql);
$i=$i+1;
}
$this->setLayout(false);
return sfView::NONE;
}