【发布时间】:2019-04-21 11:17:21
【问题描述】:
我想将我的图像保存在我使用数组的数据库 mysql 上,我可以插入一些东西,但它不是图像而是空图像。这是我的视图代码
P.S 我这里有 ajax,所以它有一个 id
这是我的观点
{!! Form::open(['action'=>'Admin\PromotionsController@store', 'method' => 'POST','enctype'=>'multipart/form-data']) !!}
<div class="form-group">
<form name="add_name" id="add_name">
<div class="table-responsive">
<table class="table table-bordered" id="dynamic_field">
<tr>
<td> {{Form::file('promotion_image[]')}}</td>
<td>{{ Form::button('Add', ['class' => 'btn btn-success', 'id'=>'add','name'=>'add']) }}</td>
</tr>
</table>
{{Form::submit('submit', ['class'=>'btn btn-primary'])}}
</div>
</form>
</div>
{!! Form::close() !!}
这是我的控制器,它将存储或保存图像
$this->validate($request, [
'promotion_image' => 'required'
]);
//Handle File Upload
if($request->hasFile('promotion_image[]')){
// Get FileName
$filenameWithExt = $request->file('promotion_image[]')->getClientOriginalName();
//Get just filename
$filename = pathinfo( $filenameWithExt, PATHINFO_FILENAME);
//Get just extension
$extension = $request->file('promotion_image[]')->getClientOriginalExtension();
//Filename to Store
$fileNameToStore = $filename.'_'.time().'.'.$extension;
//Upload Image
$path = $request->file('promotion_image[]')->storeAs('public/promotion_images',$fileNameToStore);
}else{
$fileNameToStore='noimage.jpg';
}
$promotion = new Promotion;
$promotion->promotion_image = $fileNameToStore;
$promotion->save();
我的 AJAX 代码
<script>
$(document).ready(function(){
var i=1;
$('#add').click(function(){
i++;
$('#dynamic_field').append('<tr id="row'+i+'"><td>{{Form::file('promotion_image[]',['class'=>'form-control name_list'])}}</td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>');
});
$(document).on('click', '.btn_remove', function(){
var button_id = $(this).attr("id");
$('#row'+button_id+'').remove();
});
$('#submit').click(function(){
$.ajax({
url:"name.php",
method:"POST",
data:$('#add_name').serialize(),
success:function(data)
{
alert(data);
$('#add_name')[0].reset();
}
});
});
});
</script>
【问题讨论】:
-
为什么要两次申报form?不需要第二条语句