<form id="forms-items" name="forms-items" method="post" enctype="multipart/form-data">
<input id="uploadBtn" name="uploadimg" type="file" class="upload" />
</form
在提交代码中
if($_FILES['uploadimg']['size']>0)
{
$ftype=$_FILES["uploadimg"]["type"];
if(move_uploaded_file($_FILES['uploadimg']['tmp_name'], $target_path))
{
$default=1;
$mesg="File Uploaded Successfully";
}
else
$mesg="File Uploading Failed!!";
else
$mesg="Please Select A File";
JavaScript代码
$("#forms-items").submit(function()
{
$.ajax({
url: "ajax/submitform.PHP",
type: "POST",
data: $("#forms-items").serialize(),
success: function(response) {
alert(response);
}
});
});
解决方法:
最简单的方法是使用FormData()
类:
所以现在你有一个FormData对象,随时可以与XMLHttpRequest一起发送.并使用FormData Object附加字段
<script type="text/javascript">
$(document).ready(function () {
var form = $('#forms-items'); //valid form selector
form.on('submit', function (c) {
c.preventDefault();
var data = new FormData();
$.each($(':input', form ), function(i, fileds){
data.append($(fileds).attr('name'), $(fileds).val());
});
$.each($('input[type=file]',form )[0].files, function (i, file) {
data.append(file.name, file);
});
$.ajax({
url: 'ajax/submitform.PHP',
data: data,
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: function (c) {
//code here if you want to show any success message
alert(response);
}
});
return false;
});
})
</script>
并强制jQuery不为您添加Content-Type标头,否则,将缺少上传文件边界字符串.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。