无法在表单提交时获得输入Type ='file'数据
In jQuery, when a function got success a form getting build. Now when I submit this form it gives all data except inputType='file'
. I can't get it why this is happening
here is my jQuery code when form is creating
content += '<form method="POST" action="'+formURL+'" id="data" enctype="multipart/form-data">'+
'<input type="text" name="album_id" value="'+id+'">'+
'<input type="text" name="user_id" value="'+user_id+'">'+
'<input type="file" name="image" id="image_upload">'+
'<input type="submit" value="Submit">'+
'</form>';
Here form is getting submit
$("form#data").submit(function() {
var formData = new FormData($(this)[0]);
$.post($(this).attr("action"), formData, function(data) {
console.log(data);
});
return false;
});
I am sending this form data in a controller in cakephp.
In controller I get only input field data with text
type only. But I need file
type too.
在jQuery中,当一个函数获得成功时,一个表单进行构建。 现在,当我提交此表单时,它会提供除 这是我在创建表单时的jQuery代码 p>
此表单正在提交 p>
我将此表单数据发送到cakephp中的控制器中。
In 控制器我只获得带 inputType ='file' code>之外的所有数据。 我无法理解为什么会发生这种情况 p>
content + ='&lt; form method =“POST”action =“'+ formURL +'”id =“data”enctype =“multipart / form-data”&gt;'+
'&lt; input type =“text”name =“album_id”value = “'+ id +'”&gt;'+
'&lt; input type =“text”name =“user_id”value =“'+ user_id +'”&gt;'+
'&lt; input type =“file”name =“image”id =“image_upload”&gt;'+
'&lt; input type =“submit”value =“Submit”&gt;'+
'&lt; / form&gt;';
code> pre>
$(“form#data”)。submit(function() {
var formData = new FormData($(this)[0]);
$ .post($(this).attr(“action”),formData,function(data){
console.log(data );
});
返回false;
});
code> pre>
text code>类型的输入字段数据。 但我也需要
file code>类型。 p>
div>
please use jquery.form.js for file upload.
http://malsup.com/jquery/form/
<form method="POST" action="'+formURL+'" id="data" enctype="multipart/form-data" onsubmit="return submit_form();" >
function submit_form(){
$('#data').ajaxSubmit({
method:'post',
dataType:'json',
success: function(resp){
}
});
return false;
}
You can change jquery function like this
$("form#data").submit(function(){
var formData = new FormData($(this)[0]);
$.ajax({
url: $(this).attr("action"),
type: 'POST',
data: formData,
async: false,
success: function (data) {
alert(data)
},
cache: false,
contentType: false,
processData: false
});
return false;
});