Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
444 views
in Technique[技术] by (71.8m points)

javascript - how to read FormData object in php

Hello everyone I am posting a sample code in which I am uploading a file using Ajax JQuery. All thing works fine but I want to read the file content in my php code. So what is the syntax to read it?

    <?php

?>

<!--================================html==================================================-->

<html>
<head>
    <title>AJAX UPLOAD</title>
    <script type="text/javascript" src="jquery-2.0.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $('#Button_').click(function(){
                alert("hi");
                var name= document.getElementById('File_');
                 var alpha=name.files[0];
                console.log(alpha.name);
                 var data= new FormData();
                 data.append('file',alpha);
                 $.ajax({
                 url:'process.php',
                 data:data,
                 processData:false,
                 contentType:false,
                 type:'POST',
                 success:function(msg){
                 alert(msg);
                 }
                 });
            });
        });
    </script>
</head>
<body>
    <input type="file" name="File" id="File_"/>
    <input type="button" name="Button" id="Button_" value="UPLOAD">
</body>
</html>

Now I do not know how to read the file data sent via Ajax. So please let me know the code

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The first argument you pass to FormData.append() is the name of the form element that is passed to the server. So data.append('file', alpha) in your case is equivalent to <input type="file" name="file"> - you passed 'file' to append() so 'file' is your input name.

You should find your stuff in $_POST['file'] and $_FILES['file']. Next time try var_dump($_POST); after submit to see the array. :)

Learn about file uploads in php here: http://php.net/manual/en/features.file-upload.php


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...