jQuery Ajax PHP文件上传


                    

嗨,我无法在 view.php </ code>中获取PHP FILES值,而通常可以在 view.php </ code>中找到它。 请帮帮我。 预先感谢。</ p>

这是我的代码:</ p>

  <!DOCTYPE html PUBLIC“-// W3C // DTD XHTML 1.0 Transitional // EN”“ http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<头>

&lt;/ script&gt;<br> <title>无标题文档&lt;/ title&gt;<br> <script type =“ text / javascript”><br> $(document).ready(function(){<br> $(&#39;#uploadBtn&#39;)。click(function(){<br> $ .ajax({<br> 输入:“ POST”,<br> contentType:attr(“ enctype”,“ multipart / form-data”),<br> 网址:“ view.php”,<br> 数据:$(&#39;myform&#39;)。serialize(),<br> 成功:功能(数据){<br> 警报(数据);<br>      }<br> });<br> });<br> });<br> &lt;/ script&gt;<br> &lt;/ head&gt;<br> &lt;身体&gt;<br> <form enctype =“ multipart / form-data” method =“ post” action =“ view.php” name =“ myform”><br> <input id =“ f” type =“ file” value =“” name =“ myfile” /><br> <input type =“ button” value =“ try” id =“ uploadBtn” /><br> &lt;/ form&gt;<br> &lt;/ body&gt;<br> &lt;/ html&gt;<br> &lt;/ code&gt; &lt;/ pre&gt;<br>      &lt;/ div&gt;</p>

展开原文

原文

Hi I am not able to get PHP FILES values in view.php, whereas normally it is found in view.php. Please help me out. Thanks in advance.

Here is my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<title>Untitled Document</title>
<script type="text/javascript">
$( document ).ready(function() {
$('#uploadBtn').click(function() {
$.ajax( {
type: 'POST',
contentType:attr( "enctype", "multipart/form-data" ),
url: 'view.php',
data: $('myform').serialize() ,
success: function(data){
alert(data);
    }
});
});
});
</script>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="view.php" name="myform">
<input id="f" type="file"  value=""  name="myfile"/>
<input type="button" value="try" id="uploadBtn" />
</form>
</body>
</html>

1个回答


您遇到的错误是由于尝试将“ attr”当作变量使用而引起的。 您不必设置AJAX请求的 enctype </ code>,因为它仅引用表单。 不过,您可以设置适当的 Content-type </ code>(设置为“ multipart / form-data” </ code>)</ p>


除此之外,您正在尝试使用AJAX上传文件,不可能</ strong>总是 </ em>。 请参阅此问题以供参考。</ p>

引用该问题的可接受答案:</ p>


  

使用XHR2,支持通过AJAX上传文件。 例如。 通过FormData对象,但不幸的是,所有/旧的浏览器均不支持。</ p>
</ blockquote>

我建议您尝试使用AJAX文件上传插件,它们通常做得很好,并且可以确保跨浏览器的兼容性。</ p>
     </ div>

展开原文

原文

The error you are getting is caused by trying to use "attr" as if it were a variable. You don't have to set the enctype of an AJAX request because that refers to forms only. You could set the appropriate Content-type though (to "multipart/form-data")


Aside from that, you are trying to upload a file using AJAX, which is not always possible. See this question for reference.

Quoting the accepted answer from that question:

With XHR2, File upload through AJAX is supported. E.g. through FormData object, but unfortunately it is not supported by all/old browsers.

I would recommend you try an AJAX file upload plugin, they usually do the job very well and will ensure cross-browser compatibility.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐