gpt4 book ai didi

javascript - Ajax 不发送文件

转载 作者:行者123 更新时间:2023-12-02 20:52:32 24 4
gpt4 key购买 nike

我正在尝试在 Ajax 中做一个解决方案,以在用户发送文件时发送文件并在更改状态时自动上传

代码

<script>
function filePassSelected() {
var data = $('frmpass')[0];
var formData = new FormData(data);
alert (formData);
$.ajax({
url: "upload.php",
type: "POST",
data: formData,
success:function(data){
$('#verdoc').html(data);
},
error: function(jqXHR, textStatus, errorThrown) {
alert(textStatus, errorThrown);
}
});
};
</script>
<form action="upload.php" method="post" enctype="multipart/form-data" id="frmpass">
<label for="fileToUploadPass" class="btn" id="labelfilesid">
<div class="buttonfile" style="margin:auto">National ID</div>
</label>
<input id="fileToUploadPass" style="visibility:hidden;" type="file" name="fileToUploadPass" onchange="filePassSelected()">
</form>
<div id="verdoc"></div>

否则,表单不会发送数据,我在Ajax中更改data以发送随机信息并且有效,但如果发送文件则不起作用。我做错了什么?

最佳答案

将 contentType 和 processData 添加为 false

 $.ajax({
url: "upload.php",
type: "POST",
processData: false,
contentType: false,
data: formData,
success:function(data){
$('#verdoc').html(data);
},
error: function(jqXHR, textStatus, errorThrown) {
alert(textStatus, errorThrown);
}});

关于javascript - Ajax 不发送文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61573275/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com