我正在服务器端上传图片文件,而我的脚本发出错误
错误:“没有上传文件。”
<form enctype="multipart/form-data" method="post">
<input name="file" type="file"/>
<input class="btn btn-warning" type="button" value="Upload"/>
</form>
JS代码
$(':button').click(function () {
var formData = new FormData($('form'));
$.ajax({
url: "/files/create",
type: 'POST',
success: completeHandler,
data: formData,
cache: false,
contentType: false,
processData: false
});
console.log(formData);
});
function completeHandler() {
console.log("complete success");
}
有人有个主意吗?它可以连接什么?
答案 0 :(得分:0)
试试这个:
//HTML Part
<form enctype="multipart/form-data" method="post">
<input name="file" type="file"/>
<input class="btn btn-warning" type="submit" value="Upload"/>
</form>
//Js part
<script>
$('form').submit(function (e) {
e.preventDefault();
var formData = new FormData($(this)[0]);
$.ajax({
url: '/files/create',
type: 'POST',
data: formData,
async: false,
cache: false,
contentType: false,
enctype: 'multipart/form-data',
processData: false,
success: function (response) {
alert(response);
}
});
return false;
});
</script>
php create.php
<?php
if(isset($_FILES['file'])){
print_r($_FILES);
}
?>