我有一个这样的代码形式的列表,需要用js保存数据,我的刀片文件中的代码在这里:
<input type='file' id="Pre_File">
<input type="text" id="Product_Name" class="profile-input">
<textarea type="text" id="Product_Desc" class="profile-input"></textarea>
<div id="ProSave">
<div class="card-footer">
<button type="submit">Save</button>
</div>
</div>
我用来保存Product_Name和Product_Desc的jquery在这里:
<script>
$(document).ready(function (event) {
$('#ProSave').click(function (event) {
var Product_Name = $('#Product_Name').val();
var Product_Desc = $('#Product_Desc').val();
$.post('/Product/Free/Save', {
'Product_Name': Product_Name,
'Product_Desc': Product_Desc,
'_token': $('input[name=_token]').val()
});
});
</script>
并且控制器具有以下代码:
public function Add(Request $request){
$product = new Product;
$product->product_name = $request->Product_Name;
$product->product_desc = $request->Product_Desc;
$product->save();
}
如何获取文件输入并公开移动文件夹并保存在数据库中?
答案 0 :(得分:0)
您不能使用传统的ajax发布方法发送文件,为此,您必须使用formData
对象,例如:
var variable1 = $("#elemId").val();
var fileData = $('#province_upload_image').prop('files')[0];
// Create form data object and append the values into it
var formData = new FormData();
formData.append('fileData', fileData);
formData.append('variable1', variable1);
// add as many variables you want
$.ajax({
url: $('meta[name="route"]').attr('content') + '/your_route',
method: 'post',
data: formData,
contentType : false,
processData : false,
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
success: function(response){
});
控制器:
$fileData = $request->file('fileData');
$variable1 = $request->input('variable1');
// Use it accordingly