0

我正在尝试上传文件,但我想确保它是正确的文件类型,然后再将其发送到我的上传功能。所发生的只是页面刷新,我什至没有让我的警报框显示给我。

jQuery:

<script>
$('#submit').click(function (e){
    e.preventDefault();
    if(openFile($('input#file').val()) == 1){
        window.alert("Upload Successful!");
        ajax_route(upload());
    }else{
        window.alert("Upload Failed!");
    }
    });

function ajax_route(action_requested){ 
$.post("upload_process.php", {action : action_requested}, function(data){
    if (data.length>0){ 
        alert("Hoorah! Completed the action requested: "+action_requested); 
    } 
});

function openFile(file) {
var extension = file.substr( (file.lastIndexOf('.') +1) );
if ($('input#select').val == 1){
switch(extension) {
    case 'csv':
        return 1;
    default:
        return 0;
    }
}else if ($('input#select').val == 2){
switch(extension) {
    case 'xls':
        return 1;
    default:
        return 0;
    }
}else{
switch(extension) {
    case 'doc':
        return 1;
    default:
        return 0;
    }
}
};
</script>

HTML 表单:

<div id="fileupload">
<form name="fileupload" id="fileupload" action="" method="POST" enctype="multipart/form-data">
<input type="text" name="select" id="select" value="" style="display:none;">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" id="submit" name="submit" value="Submit">
</form>
</div>

PHP上传功能:

<?php
function upload(){
$select = $_POST["select"];

if($select == "1"){
    $path = "docs/tmtmasterdata.csv";
}elseif ($select == "2"){
    $path = "docs/A.2012_TMT_Vendor_Form.xls";
}elseif ($select == "3"){
    $path = "docs/z.Credit_Card_Authorization.doc";
}elseif ($select == "4"){
    $path = "docs/z.TMT_Customer_Credit_App.doc";
}

  if($_FILES['file'] != null){
    if(copy($_FILES['file']['tmp_name'],$path)){
        ?><script>window.alert("Upload Successful!");</script><?php
    }else{
        ?><script>window.alert("Upload Failed!");</script><?php
    }
  }
}
?>
4

0 回答 0