Reputation: 411
I am trying to upload a file. I did as usual but when I click the submit button it's not getting to the post route instead the page is just refreshing. Is there anything wrong with my filecontroller? besides the method which I am applying for uploading a file, is it correct?
add_file.blade.php:
<form class="form-horizontal" action="{{url('upload_file')}}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<fieldset>
<div class="control-group">
<label class="control-label" for="date01">File name</label>
<div class="controls">
<input type="text" class="input-xlarge" name="file_name" required="" >
</div>
</div>
<div class="control-group" >
<label class="control-label" for="selectError3">Department name</label>
<div class="controls" >
<select id="selectError3" name="department_id"style=" width: 200px">
<option>select department</option>
<?php
$all_department=DB::table('dept')
->get();
foreach($all_department as $v_department){?>
<option value="{{$v_department->department_id}}">{{$v_department->department_name}}</option>
<?php } ?>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="selectError3">Session </label>
<div class="controls">
<select id="selectError3" name="session_id">
<option>select Session</option>
<?php
$all_session=DB::table('session')
->get();
foreach($all_session as $v_session){?>
<option value="{{$v_session->session_id}}">{{$v_session->session_name}}</option>
<?php } ?>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="selectError3">Semester</label>
<div class="controls">
<select id="selectError3" name="semester_id">
<option>select semester</option>
<?php
$all_semester=DB::table('semester')
->get();
foreach($all_semester as $v_semester){?>
<option value="{{$v_semester->semester_id}}">{{$v_semester->semester_name}}</option>
<?php } ?>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="selectError3">Course </label>
<div class="controls">
<select id="selectError3" name="course_id">
<option>select course </option>
<?php
$all_course=DB::table('course')
->get();
foreach($all_course as $v_course){?>
<option value="{{$v_course->course_id}}">{{$v_course->course_code}}</option>
<?php } ?>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="fileInput">Upload file</label>
<div class="controls">
<input class="input-file uniform_on" name="file_any" type="file" required="">
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Add File</button>
<button type="reset" class="btn">Cancel</button>
</div>
</fieldset>
</form>
fileController@store file:
public function store(Request $request)
{
$this->validate($request,[
'file_name' =>'required',
'file_path' =>'required',
'department_id' => 'required',
'session_id' => 'required',
'semester_id' => 'required',
'course_id' => 'required',
]);
$file = $request->file('file_any');
$name= $file->getClientOriginalName();
$extension= $file->getClientOriginalExtension();
$size = $file->getClientSize();
$newName= $name . '.' . $extension;
$path= Storage::putFileAs('public',$request->file('file_any'),$newName);
$files= File::create([
'file_name' => $newName,
'file_path' => $path,
'department_id' => $request->department_id,
'session_id' => $request->session_id,
'semester_id'=> $request->semester_id,
'user_id' => Auth::id(),
'course_id' => $request->course_id,
'file_ext' => $extension,
'file_size' => $size,
]);
Session::put('message','File added successfully !!!');
return redirect()->back();
}
Routes:
Route::post('upload_file', 'fileController@store');
Route::get('/add_files', 'fileController@create');
Upvotes: 0
Views: 46
Reputation: 4556
Seems that the file_path
is required in your validation. You have to add file_path
input in your form. Also can you try to add displaying of errors in the view form. e.g. add this on your view form:
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
Upvotes: 1