繁体   English   中英

以Ajax格式上传文件仅发送名称,不发送文件数据

[英]Upload file in Ajax Form only sends name, not file data

好吧,我很困惑。 我有一个ajax方法:

 function check_empty() { if (document.getElementById('name').value == "" || document.getElementById('email').value == "" || document.getElementById('msg').value == "") { alert("Fill All Fields !"); } else { //alert("Will submit form"); // e.preventDefault(); $('#spinner').show(); $('#quote_form').hide(); $('#submit').hide(); var formData = new FormData(); formData.append("file", $("#resume").val()); $.ajax({ type: 'post', url: '/contact_post.php', data: formData, success: function(html) { alert('Enquiry Submitted. Thank You!'); }, error: function(xhr, status, error) { var jsonResponseText = $.parseJSON(xhr.responseText); var message = ''; alert('could not submit form: ' + jsonResponseText['message']); }, //Options to tell jQuery not to process data or worry about content-type. processData: false, contentType: false }); } 

Firefox下的请求参数说:----------------------------- 4894670511610358147552973488 Content-Disposition:表单数据; name =“文件”

test.pdf ----------------------------- 4894670511610358147552973488--

但是实际的文件数据在哪里? 我似乎无法“发送文件”,另一端却有PHP可以接收它。

使用ajax发布文件时,请确保获得对该文件的有效引用。 通常这看起来像:

formData.append("file", document.getElementById("resume").files[0]);

另外,您可能需要将内容类型设置为处理文件,因此类似:

ajaxRequest.setRequestHeader("Content-Type", "multipart/form-data");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM