以下是我的ajax功能
$(document).on('click','.formsubmitbtn',function(){
var owner = $("#formowner").val();
var remark = $("#formremark").val();
var unreline_id = $("#formurelineid").val();
var cat = $("#formcat").val();
var action = $(this).attr('action');
var doc = $("#doc").val();
$.ajax({
"url":"{{url('formsubmit')}}",
"type":"POST",
"data":{
"owner":owner,
"remark":remark,
"unreline_id":unreline_id,
"cat":cat,
"action":action,
"doc":doc
},
success:function(result){
console.log(result);
}
});
});
其中doc是文件。
如何将文件值发送到控制器。
Plz帮助。
答案 0 :(得分:1)
这样做。
<form id="upload">
<input type="file" name="file">
<button type="submit">Upload</button>
</form>
<script>
$( document ).ready(function() {
$("#upload").submit(function(e){
e.preventDefault();
var form=($(this)[0]);
var formdata=new FormData(form);
$.ajax({
url:'url.php',//url to route for a controller
type:'POST',
data:formdata,
enctype:'multipart/form-data',
processData:false,
contentType:false,
success:function(data){
//if upload will succesful
},
error: function (data) {
//if upload fails
}
});
});
});
</script>