【发布时间】:2018-12-28 01:41:35
【问题描述】:
我正在尝试插入 2 个文件 image 和 video 以及作为数组的附加图像,我现在可以同时插入这两个文件,但是当我尝试将另一个图像添加为数组并保存时,输出也是插入在表单上选择的视频。如何将图像数组保存为独立图像并删除自动插入的视频
这是我的用户界面,请查看我的页面,我将其另存为图片并再次添加图片
这是我的数据库输出
我想要的输出是绿灯和黄灯应该是单一的,并且 没有插入视频,因为在页面上它们只是数组而不连接到视频
现在这是我的控制器代码
$this->validate($request, [
'paxsafety_image.*' => 'nullable|image|mimes:jpeg,jpg,png',
'paxsafety_video.*' => 'nullable|mimes:mp4,mov,ogg | max:20000'
]);
$paxSafety = [];
$paxSafetyVideo = [];
if ($request->has('paxsafety_image') && $request->has('paxsafety_video'))
{
//Handle File Upload
foreach ($request->file('paxsafety_image') as $key => $file)
{
// Get FileName
$filenameWithExt = $file->getClientOriginalName();
//Get just filename
$filename = pathinfo( $filenameWithExt, PATHINFO_FILENAME);
//Get just extension
$extension = $file->getClientOriginalExtension();
//Filename to Store
$fileNameToStore = $filename.'_'.time().'.'.$extension;
//Upload Image
$path = $file->storeAs('public/paxsafety_folder',$fileNameToStore);
array_push($paxSafety, $fileNameToStore);
}
foreach ($request->file('paxsafety_video') as $key => $file)
{
// Get FileName
$filenameWithExt2 = $file->getClientOriginalName();
//Get just filename
$filename = pathinfo( $filenameWithExt2, PATHINFO_FILENAME);
//Get just extension
$extension2 = $file->getClientOriginalExtension();
//Filename to Store
$fileNameToStore2 = $filename.'_'.time().'.'.$extension2;
//Upload Image
$path = $file->storeAs('public/paxsafety_folder',$fileNameToStore2);
array_push($paxSafetyVideo, $fileNameToStore2);
}
$fileNameToStore = serialize($paxSafety);
$fileNameToStore2 = serialize($paxSafetyVideo);
}
else
{
$paxSafety[] = 'noimage.jpg';
$paxSafetyVideo[] = 'noimage.jpg';
}
foreach ($paxSafety as $key => $value) {
$paxSafetyContent = new PaxSafety;
$paxSafetyContent->paxsafety_image = $value;
foreach ($paxSafetyVideo as $key => $values) {
$paxSafetyContent->paxsafety_video = $values;
}
$paxSafetyContent->save();
}
这是我的查看代码
{{ Form::file('paxsafety_image[]') }} <strong> <span class="fa fa-camera"></span> Upload Image </strong>
{{ Form::file('paxsafety_video[]') }} <strong> <span class="fa fa-video-camera"></span> Upload Video </strong>
我的脚本代码
<script>
$(document).ready(function(){
var i=1;
$('#add').click(function(){
i++;
$('#dynamic_field').append('<tr id="row'+i+'"><th>Upload new Image</th><td>{{ Form::file('paxsafety_image[]') }} <button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove fa fa-minus-circle"></button></td></tr>');
});
$(document).on('click', '.btn_remove', function(){
var button_id = $(this).attr("id");
$('#row'+button_id+'').remove();
});
$('#submit').click(function(){
var form = $('#add_name')[0];
var formData = new FormData(form);
$.ajax({
method:"POST",
data:formData,
success:function(data)
{
alert(data);
$('#add_name')[0].reset();
}
});
});
});
</script>
另外,这是我的表架构
$table->string('paxsafety_image')->nullable();
$table->string('paxsafety_video')->nullable();
【问题讨论】:
-
是您的
upload new image and video部分选择单个image和video吗?
标签: javascript jquery mysql ajax laravel