dsozqcx9668
2016-04-20 04:47
浏览 164
已采纳

在php中检索formdata .append方法

I upload text/plain file and pass it to php do something via jquery ajax. However, jquery ajax return jquery-2.2.3.js:8998 Uncaught TypeError: Illegal invocation FormData not working in jquery return uncaught type error

Check jquery code which look like below enter image description here

<input type="file" id="file">
<input type="submit" value="submit"  id="btnUpload">

<script>
$(document).ready(function(){

$('#btnUpload').click(function(){

    var ajaxData = new FormData();
    ajaxData.append('action', 'scan_email');
    ajaxData.append('file', $('#file')[0].files[0]);
    $.ajax({
        url: 'ajax.php',
        data: ajaxData,
        method: 'POST',
//          beforeSend: function(){ 
//              // alert('uploading..');
//          },
        success: function(data){
            alert(data);
        },
        error: function(err){
            alert('error');
        }
        });

    });

});
</script>

ajax.php

<?php

echo $_POST['action'];

if (isset($_POST['action'])) {
    switch ($_POST['action']) {
        case 'scan_email':
            scan_email();
            break;
    }
}

function scan_email(){

    if ($_FILES["file"]["error"] > 0)
        echo "Error: " . $_FILES["file"]["error"] . "<br />";
    else if ($_FILES["file"]["type"] !== "text/plain")
        echo "File must be a .txt";
    else{
        //do something
        echo 'success';
        // $file_handle = fopen($_FILES["file"]["name"], "rb");
       }
    exit;
}

?>

图片转代码服务由CSDN问答提供 功能建议

我上传text / plain文件并将其传递给php通过jquery ajax做一些事情。 但是,jquery ajax返回 jquery-2.2.3.js:8998 Uncaught TypeError:非法调用</ strong> </ p> \ n

检查jquery代码,如下所示 </ p>

 &lt; input type =”file“id =”file“  &gt; 
&lt; input type =“submit”value =“submit”id =“btnUpload”&gt; 
 
&lt; script&gt; 
 $(document).ready(function(){
 
 $ n('  #btnUpload')。click(function(){
 
 var ajaxData = new FormData(); 
 ajaxData.append('action','scan_email'); 
 ajaxData.append('file',$(  '#file')[0] .files [0]); 
 $ .ajax({
 url:'ajax.php',
 data:ajaxData,
 method:'POST',
 //  beforeSend:function(){
 // // alert('uploa  ding ..'); 
 //},
 success:function(data){
 alert(data); 
},
 error:function(err){
 alert('error');  
} 
}); 
 
}); 
 
}); 
&lt; / script&gt; 
 </ code> </ pre> 
 
 

ajax.php </ p>

 &lt;?php 
 
echo $ _POST ['action']; 
 
if(isset($ _ POST ['action'])){
 switch($  _POST ['action']){
 case'scan_email':
 scan_email(); 
 break; 
} 
} 
 
function scan_email(){
 
 if($ _FILES [“file  “] [”错误“]&gt;  0)
 echo“错误:”。  $ _FILES [“file”] [“error”]。  “&lt; br /&gt;”; 
 if if($ _FILES [“file”] [“type”]!==“text / plain”)
 echo“File必须是.txt”; 
 else  {
 //做某事
 echo'adccess'; 
 // $ file_handle = fopen($ _ FILES [“file”] [“name”],“rb”); 
} 
退出; 
  } 
 
?&gt; 
 </ code> </ pre> 
 </ div>

1条回答 默认 最新

相关推荐 更多相似问题