html代码:
<div class="sc_btn_box">
<input type="file" class="sc_btn" id="file">
<span class="file_uploader">选择</span>
</div>
<div class="m_img_holder"></div>
<div id="show_msg" style="margin-left:80px;"></div>
<div class="xz_btn">
<div class="btn btn-success queding" id="submit">确定</div>
<div class="btn btn-success quxiao">取消</div>
</div>
JS代码:
$(function () {
$("#submit").click(function () {
if ($("#file").val().length > 0) {
ajax_upload();
}else {
alert("请选择文件");
}
})
})
function ajax_upload(){
var formData = new FormData();
formData.append('file', $('#file')[0].files[0]);
$.ajax({
url:"http://127.0.0.1/newsys/upload.php?action=upload_file",
type:"post",
data:formData,
processData:false,
contentType:false,
success:function(data){
alert(data);
$('.tc_box').hide();
},
error:function(e){
alert("错误!!");
}
});
}
后台代码php,用$_FILES来获取文件的信息,和其他上传方式一样
function upload_file()
{
$username = $_POST['username'];
$account = $_POST['account'];
$upload_type = $_POST['upload_type'];
$dir = './upload_file/';
$pic_name = $_FILES['file']['name'];
$pic_name_arr = explode('.', $_FILES['file']['name']);
$suffix_str = '.' . $pic_name_arr[count($pic_name_arr)-1];
$upfile = str_replace($suffix_str, '_' . date('Y-m-d H:i:s',time()) . $suffix_str, $pic_name);
$upfile = $dir . $upfile;
if ($_FILES['file']['error'] > 0) {
switch ($_FILES['file']['error']) {
case 1:
echo '文件大小超过服务器限制';
break;
case 2:
echo '文件太大!';
break;
case 3:
echo '文件只加载了一部分!';
break;
case 4:
echo '文件加载失败!';
break;
}
exit;
}
if ($_FILES['file']['size'] > 2000000) {
echo '文件过大,不能超过2M!';
exit;
}
if (is_uploaded_file($_FILES['file']['tmp_name'])) {
$handle = fopen($_FILES['file']['tmp_name'],"r");
$file_encoding = mb_detect_encoding($handle);
if ($file_encoding != 'ASCII'){
// echo "<script type=\"text/javascript\">alert(\"文件编码错误,请重新上传!\"); </script>";
echo "文件编码错误,请重新上传";
exit;
}
if (!move_uploaded_file($_FILES['file']['tmp_name'], $upfile)) {
echo '上传文件失败!';
exit;
}
// if ($suffix_str == '.csv') {
// file_to_db_csv($upfile,$username,$account,$upload_type);
// } else {
file_to_db($upfile,$username,$account,1);
// }
} else {
echo '上传文件错误!';
exit;
}
closedir($dir);
// } else {
// echo "已存在数据文件!";
// }
}
上传成功后,得到文件的绝对路径$upfile,如果是excle文件,还可以通过file_to_db写入到数据库中;
function file_to_db($file_path,$username,$account,$upload_type){
include_once '../weixin/tongji/common_func/PHPExcel_1.8.0/PHPExcel.php';
include_once '../weixin/tongji/common_func/PHPExcel_1.8.0/PHPExcel/IOFactory.php';
include_once '../weixin/tongji/common_func/PHPExcel_1.8.0/PHPExcel/Shared/Date.php';
setlocale(LC_ALL, 'zh_CN.GBK');
global $_weixin_guanggao_guanzhu_num_record;
if (file_exists($file_path)) {
$objReader1 = PHPExcel_IOFactory::createReaderForFile($file_path);
$objPHPExcel1 = $objReader1->load($file_path);
$col_str = $objPHPExcel1->getSheet(0)->getHighestColumn();
$first_col=ord('A');// ASSCLL码
$real_col=ord($col_str);// ASSCLL码
$field_arr = array('date','num');
$real_col_count = count($field_arr)+$first_col-1;
if ($real_col > $real_col_count) {
echo "导入失败,文件数据列{$real_col}超过{$type_arr[$upload_type]}所规定的列数";
exit();
}
$suc_count = 0;
$suc_id_arr = array();
$edit_id_arr = array();
$edit_count = 0;
$msg= array();
for ($i = 2; $i <= $objPHPExcel1->getSheet(0)->getHighestRow(); $i++) {
// $col_str = chr(65);// A
$add = array();
for ($jj=0; $jj < count($field_arr); $jj++) {
$field_str = $field_arr[$jj];
$add[$field_str] = iconv('utf-8', 'gbk', $objPHPExcel1->getActiveSheet()->getCell(chr(65+$jj).$i)->getValue());
}
// 把date的日期格式转为时间戳
$add['date_timestamp'] = PHPExcel_Shared_Date::ExcelToPHP($add['date']);
$add['date'] = date("Y-m-d",$add['date_timestamp']);
$add['post_date'] = time();
if(empty($add[$field_arr[0]])){
$msg['upload_user'] = $username;
$msg['account'] = $account;
$msg['add_ids'] = implode(',', $suc_id_arr);
$msg['edit_ids'] = implode(',', $edit_id_arr);
$msg['type'] = $upload_type;
$msg['msg'] = "新增:".$suc_count.",修改:".$edit_count;
$msg['filename'] = $file_path;
$msg['post_date'] = time();
// $_wy100_weixin_ad_upload_record->add($msg);
echo "<script>alert('导入完毕,新增:".$suc_count.",修改:".$edit_count."');";
exit();
}
$check_list = $_weixin_guanggao_guanzhu_num_record->_list(0,1, "id", "where date_timestamp = ".$add['date_timestamp']);
if (!$check_list) {
$this_id = $_weixin_guanggao_guanzhu_num_record->add($add);
$suc_count++;
$suc_id_arr[] = $this_id;
} else {
$this_id = $_weixin_guanggao_guanzhu_num_record->edit($add,$check_list[0]['id']);
$edit_count++;
$edit_id_arr[] = $check_list[0]['id'];
}
}
echo "导入完毕,新增:".$suc_count.",修改:".$edit_count;
} else {
// echo "<script>alert('导入失败,不存在上传文件');window.location.href='http://weixin.wy100.com/tongji/tongji_admin/index.php';</script>";
echo "导入失败,不存在上传文件";
}
exit();
}