我已经创建了一个PHP上传页面,以便将xls文件从pc上传到MySQL表,除了两个问题外,整个过程可以很好地工作。 假设正在进行上传,意外关闭浏览器或点击其他链接的用户;在这种情况下,部分项目上传到MySQL表,其余部分都失败。如何强制用户等到上传完成,或者创建一个确认框,如果它返回false,它将删除已上传的项目。
另一个问题 - 有一种方法我可以在提交后停用上传按钮,我已经搜索了谷歌并找到了许多解决方案,但它们都不适用于Chrome浏览器。
如果你想在这里是我的上传PHP代码:
这表示提交表单后的Loader gif
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script>
<!--Loader-->
function getVal(){
$('#load').load('loader.htm');
}
<!--End of Loader-->
</script>
上传文件
<form enctype="multipart/form-data" method="post" >
<table width="100%" height="%" align="center" border="1" cellpadding="4" cellspacing="0">
<tr><td align="center" colspan="2"><img src="images/import.png" width="108" height="108"></td></tr>
<tr>
<td><input type="file" name="file" id="file"></td></tr>
<tr >
<td colspan="2" align="center" height="70px"><br /><br /><input type="submit" onClick="getVal()" name="Import_items" value="Import Items" style="background-color:orange; font-weight:bold; font-size:16px; width:200px; height:50px;"></td>
</tr>
</table>
</form>
<center>
<div id="load"></div>
</center>
提交后
if(isset($_POST["Import_items"]))
{
$filename=$_FILES["file"]["tmp_name"];
if(empty($filename)){
?>
<script>
alert('Error : Please Select Excel File to Import !');
location.replace('import.php');
</script>
<?php
exit();
}
//Check File Extension if Valid or Not (Excel Only Format)
$allowed = array('csv','xls','xlsx');
$ff = $_FILES['file']['name'];
$ext = pathinfo($ff, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed) ) {
echo "<center>Extension is Not Excel !</center>";
?>
<script>
alert('Error : Please Upload Correct Excel File Extension');
location.replace('import.php');
</script>
<?php
exit();
}
$date = date('Y-m-d');
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';
$inputFileName = $filename; // File to read
//echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory to identify the format<br />';
try {
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
} catch(Exception $e) {
die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$num = count($sheetData);
for($i=2;$i<=$num;$i++){
if($sheetData[$i]['B'] ==0 && $sheetData[$i]['C'] ==0){
Something 1
}
else {
//Something 2
if(!empty($sheetData[$i]['E']) && (!empty($sheetData[$i]['E']))){
//Do Something
}
else{
// Do something Else
}
}
}
}
//For Loop end
}
谢谢大家,任何帮助都将不胜感激!
答案 0 :(得分:0)
我喜欢你到目前为止所做的一切。非常好地思考预测用户行为。
我建议使用jquery进度条。
http://www.sitepoint.com/html5-javascript-file-upload-progress-bar/
当然你还需要一个'onwindow close'检测,以便你可以抛出一个'确认'对话框。
$(window).unload(function() {
//do something
}
然后你应该有你的功能。
在服务器端保持干净。只要成功或失败就应该这样做。
除此之外......非常好的工作:)
在后端试试这个:
class Uploader
{
$responses = [
'Error : Please Upload Correct Excel File Extension',
'Error : Please Select Excel File to Import !',
'Success: Your file has been processed & uploaded.'
];
function returnResponses($response)
{
return sprintf("<script>alert('%s');
location.replace('import.php');
</script>", $responses[$response]);
}
function checkUpload($file)
{
$filename=$file["tmp_name"];
if(isset($_POST["Import_items"]))
{
if(empty($filename))
{
return $this->returnResponses(1);
}
}
else
{
$allowed = array('csv','xls','xlsx');
$ff = $file['name'];
$ext = pathinfo($ff, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed) )
{
return "<center>Extension is Not Excel !</center>" . $this->returnResponses(0);
}
else{
//success & process files
$date = date('Y-m-d');
//process your file
return $this->returnResponses(2);
}
}
}
}
$uploader = new Uploader();
return $uploader->checkUpload($_FILES['file']);