【问题标题】:file is not uploading in ajax php mysql文件未在 ajax php mysql 中上传
【发布时间】:2014-03-11 02:44:35
【问题描述】:

我正在尝试使用 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";
  }

?>

【问题讨论】:

  • 你应该在这个问题中看到答案:stackoverflow.com/questions/166221/…,你可能会在那里找到解决方案
  • 是的,这个问题很有帮助,但我必须寻找很多才能找到我的错误,非常感谢@Sylvain
  • 但还是没找到问题所在。
  • var form_data = $('#reg_form').serialize();之后检查form_data的值,你应该会发现问题。
  • serialize 仅在您发送文本数据时有用。由于您正在发送文件,因此您应该尝试使用FormData,如我在几个 cmets 前链接的问题的已接受答案中所述。

标签: php jquery mysql ajax


【解决方案1】:

首先,serialize() 函数不适用于文件,您应该创建一个表单对象,您可以通过该对象发布数据并且可以完美运行我遇到了同样的问题,我刚刚解决了您的问题并且是100% 工作,因为我已经对此进行了测试。请检查。 表格。

<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";
  }

?>

【讨论】:

  • 非常感谢,非常感谢,您让我的一天工作得很好,现在我必须仔细阅读这篇文章,非常感谢
猜你喜欢
  • 2014-03-10
  • 2015-07-16
  • 2012-04-22
  • 2014-03-22
  • 2012-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-01
相关资源
最近更新 更多