weixin_33738555 2014-03-28 12:11 采纳率: 0%
浏览 61

jQuery Ajax PHP文件上传

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条回答 默认 最新

  • weixin_33724059 2014-03-28 13:02
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!