2017-09-25 132 views
0

我无法上传图像文件到MySQL数据库后提交表单。我收到错误消息:我无法上传图像文件到mysql数据库使用ajax jquery php

Uncaught TypeError: Illegal invocation

在我的检查元素控制台选项卡中。

<!DOCTYPE html> 

<html> 
<head> 
    <title>Ajax Test</title> 
    <style> 
     .error{ 
      color: red; 
     } 
     .success{ 
      color: green; 
     } 
     .input-error { 
      box-shadow: 0 0 5px red; 
     } 
     .input-success { 
      box-shadow: 0 0 5px green; 
     } 
    </style> 

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script> 
    <script> 
     $(document).ready(function() { 
      $('form').submit(function (event) { 
       event.preventDefault(); 
       var inp = $("#t_input").val(); 
       var file = $("#t_file").val(); 
       var sub = $("#t_submit").val(); 
       $(".test_msg").load("hello_ajax.php", { 
        inp: inp, 
        type: "POST", 
        url: "hello_ajax.php", 
        file: new FormData($(file)[0]), 
        contentType: false, 
        cache: false, 
        processData: false, 
        success: function(feedback){ 
         $("#t_file").val(""); 
        }, 
        sub: sub 
       }); 
      }); 
     }); 
     </script> 

</head> 
<body> 
    <h2>Testing Ajax</h2> 
    <form action="hello_ajax.php" method="POST"> 
     <input id="t_input" type="text" name="tst_input" placeholder="Hello" /> 
     <input id="t_file" type="file" name="tst_file"/><br/> 
     <input id="t_submit" name="submit" type="submit" value="submit" /><br/> 
     <p class="test_msg"></p> 
    </form> 
</body> 
</html> 

下面是上传文件MySQL数据库下面的PHP代码重定向

<?php 
    $host = "127.0.0.1"; 
    $id = "root"; 
    $pass = ""; 
    $db = "test_mysql"; 

    try{ 
     $conn = new PDO("mysql:host=$host; dbname=$db", $id, $pass); 
     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

    }catch(PDOException $e){ 
     echo "Error" . $e->getMessage(); 
    } 

    if (isset($_POST['sub'])){ 


     if(isset($_FILES["t_file"])){ 
      $file = $_FILES['t_file']; 
      $sql = "ISERT INTO uploadimage (imgName) VALUES (:img)"; 
      $stmt->prepare($sql); 
      $stmt->bindValue(":img", $file, PDO::PARAM_STR); 
      $stmt->execute(); 
     }else{ 
      echo "<span class='error'> Unable to input file </span><br/>"; 
     } 

     $in = $_POST["inp"]; 

     $blk = false; 

     if (empty($in)){ 
      echo "<span class='error'>Field must not be empty! </span>"; 

      $blk = true; 
     }else{ 
      echo "<span class='success'>" . $in . "Successfull</span>"; 

     } 
    }else{ 
     echo "<span class='test_msg'>There is some Error </span>"; 

    } 
?> 
<script> 
    $("#test_input").removeClass("input-error"); 
    var blk = "<?php echo $blk; ?>"; 

    if(blk == true){ 
     $("#t_input").addClass("input-error"); 
     $("#t_input").val(""); 
    } 
    if(blk == false){ 
     $("#t_input").val(''); 
    } 
</script> 
+2

可能重复[如何异步上传文件?](https://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously) –

回答

0

后的$_FILES阵列包含有关文件,而不是文件本身的元数据。实际文件存储在$_FILES['t_file']['tmp_name']的临时文件中。你可以使用

$file = file_get_content($_FILES['t_file']['tmp_name']); 

检查documentation为阵的完整结构的内容。

+0

您应该检查'$ _FILES ['t_file '] ['error']'也尝试使用该文件。 – solarc