【发布时间】:2016-03-22 17:56:08
【问题描述】:
我正在尝试使用 FormData 通过 AJAX 上传文件。如果我在没有选择要上传的任何文件的情况下提交 AJAX 调用,则帖子可以正常工作,并且服务器上会收到其他字段(不是文件上传)。但是,如果我选择要上传的文件,则调用到达服务器时没有任何数据(在 PHP 中,$_POST 和 $_FILES 数组都是完全空的)。我知道如果你没有告诉 jQuery 不要设置 contentType,就会发生这种情况,但是我将 contentType 和 processData 设置为 false,它仍然不会发送数据。
这是我的代码:
function AddComment(taskid) {
var newnote = $('#newnote_'+taskid).val();
if(newnote != '') {
$('#tasklist *').css('cursor', 'progress');
var formData = new FormData();
$('.upload-' + taskid).each(function() {
if (this.files[0]) {
formData.append($(this).attr('name'), this.files[0]);
}
});
formData.append("taskid", taskid);
formData.append("newnote", newnote);
$.ajax({
url: '/modules/task/ajax/ajaxAddComment.php',
data: formData,
processData: false,
contentType: false,
type: 'post',
success: function(data){
alert(data);
}
});
}
}
我确定我在做一些愚蠢的事情,但我看不到什么......?
编辑:这是 HTML:
<form id="frmNewComment544" enctype="multipart/form-data" method="post" action="">
<div>
<textarea style="width:100%;" cols="30" rows="5" id="newnote_544"></textarea>
</div>
<div>
<input type="button" onclick="AddComment(544)" value="Append Comment">
</div>
<div class="attachment-browsers" id="attachmentBrowsers544" style="display: block;">Attachments will be uploaded when you append a comment.
<div>
<input type="file" id="upload_544_151ab3cfe69" name="upload_544_151ab3cfe69" class="upload-544">
</div>
<div>
<input type="file" id="upload_544_3y4afe6eg7a" name="upload_544_3y4afe6eg7a" class="upload-544">
</div>
</div>
</form>
编辑 2:好的,只有在上传相对较大的文件时才会出现问题(不是很大 - 在本例中为 10MB)。小文件上传OK。那么现在的问题是为什么我不能使用这种方法上传大文件?
【问题讨论】:
-
你也可以张贴
html表格吗? -
请用console.log()检查所有参数,检查任何失败都会破坏post data send failed。
标签: php jquery ajax file-upload form-data