小编典典

文件没有在ajax php mysql中上传

ajax

我正在尝试使用ajax上传文件,这给了我一个错误,其余的数据上传成功了,我尝试了不使用ajax进行文件上传,但是当我尝试通过ajax上传文件时给了我错误,我完全困惑为什么ajax给我问题。这是我的代码。

<html>
<head>
<script src="jquery-1.8.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#button").click(function(){
 var form_data = $('#reg_form').serialize();
$.ajax({
    type:"POST",
    url:"process.php",
    data:form_data,
    success: function(data)
    {
        $("#info").html(data);
    }
});
});

});
</script>
</head>

   <body>
        <form id="reg_form" enctype="multipart/form-data" method="post" action="">
               name : <input type="text" name="name" id="name"/>
               </br>
               message : <input type="text" name="message" id="message" />
               </br>
               Image : <input type="file" name="file" id="file" />
               <input type="button" value="Send Comment" id="button">

               <div id="info" />
        </form>
   </body>
</html>

process.php文件编码在这里。

<?php
  mysql_connect("localhost","root","");
  mysql_select_db("ajaxdatabase");

  $name=$_POST["name"];
  $message=$_POST["message"];
  //storing file in filename variable
    $fileName = $_FILES['file']['name'];
    //destination dir
    $to="image/".$fileName;

    move_uploaded_file($_FILES['file']['tmp_name'],$to);

  $query=mysql_query("INSERT INTO common(name,message,destination) values('$name','$message','$to') ");

  if($query){
    echo "Your comment has been sent";
  }
  else{
    echo "Error in sending your comment";
  }

?>

阅读 392

收藏
2020-07-26

共1个答案

小编典典

首先,serialize()函数不适用于文件,您应该使对象成为可通过其发布数据的表单对象,并且可以完美地工作。因为我已经测试过了 请退房。表格。

<form name="multiform" id="multiform" action="process.php" method="POST" enctype="multipart/form-data">
               name : <input type="text" name="name" id="name"/>
               </br>
               message : <input type="text" name="message" id="message" />
               </br>
               Image : <input type="file" name="file" id="file" />
        </form>
               <input  type="button" id="multi-post" value="Run Code"></input>
               <div id="multi-msg"></div>

剧本。

<script type="text/javascript">
$(document).ready(function(){
$("#multiform").submit(function(e)
{
    var formObj = $(this);
    var formURL = formObj.attr("action");

if(window.FormData !== undefined)  
    {
        var formData = new FormData(this);
        $.ajax({
            url: formURL,
            type: 'POST',
            data:  formData,
            mimeType:"multipart/form-data",
            contentType: false,
            cache: false,
            processData:false,
            success: function(data, textStatus, jqXHR)
            {
                    $("#multi-msg").html('<pre><code>'+data+'</code></pre>');
            },
            error: function(jqXHR, textStatus, errorThrown) 
            {
                $("#multi-msg").html('<pre><code class="prettyprint">AJAX Request Failed<br/> textStatus='+textStatus+', errorThrown='+errorThrown+'</code></pre>');
            }           
       });
        e.preventDefault();
        e.unbind();
   }
});
$("#multi-post").click(function()
    {
    //sending form from here
    $("#multiform").submit();
});

});

</script>

您的php文件与我测试过的文件相同,并且正在运行。

<?php
  mysql_connect("localhost","root","");
  mysql_select_db("ajaxdatabase");

  $name=$_POST["name"];
  $message=$_POST["message"];
  //storing file in filename variable
    $fileName = $_FILES['file']['name'];
    //destination dir
    $to="image/".$fileName;

    move_uploaded_file($_FILES['file']['tmp_name'],$to);

  $query=mysql_query("INSERT INTO common(name,message,destination) values('$name','$message','$to') ");

  if($query){
    echo "Your comment has been sent";
  }
  else{
    echo "Error in sending your comment";
  }

?>
2020-07-26