2017-09-25 85 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