i'm working in a project where i wanted to upload an image for Category. The uploading part is working smooth. what i want is before uploading, when user selects the image a preview of the selected image along with name of the image should be shown. I'm pretty lost in this case.
Below is my view part:
{!! Form::open(['url' => '/addCategory', 'method' => 'post', 'enctype'=> 'multipart/form-data']) !!}
{{ csrf_field() }}
@method('post')
<div class="card ">
<div class="card-header card-header-primary">
<h4 class="card-title">{{ __('Add Category') }}</h4>
<p class="card-category"></p>
</div>
<div class="card-body ">
<div class="row">
<label class="col-sm-2 col-form-label">{{ __('Name') }}</label>
<div class="col-sm-7">
<div class="form-group{{ $errors->has('name') ? ' has-danger' : '' }}">
<input class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" name="category" id="category" type="text" placeholder="{{ __('Name') }}" value="" required="true" aria-required="true" />
</div>
</div>
</div>
<div class="row">
<label class="col-sm-2 col-form-label">{{ __('Description') }}</label>
<div class="col-sm-7">
<div class="form-group{{ $errors->has('name') ? ' has-danger' : '' }}">
<textarea class="md-textarea form-control" rows="5" name="desc" id="desc" type="text" placeholder="{{ __('Description') }}" value="" required="true" aria-required="true"></textarea>
</div>
</div>
</div>
<div class="row">
<label for="image" class="col-sm-2 col-form-label">Category Image</label>
<div class="col-sm-7">
<input id="cat_image" type="file" class="form-control" name="cat_image">
<img src="" id="category-img-tag" width="200px" /> <!--for preview purpose -->
</div>
</div>
</div>
<div class="card-footer ml-auto mr-auto">
<button type="submit" class="btn btn-primary">{{ __('Add Category') }}</button>
</div>
</div>
Below is the controller part:
public function upload(Request $request)
{
$cat = new Category;
$cat->name = request('category');
$cat->description = request('desc');
if ($request->file('cat_image'))
{
$categoryFile = $request->file('cat_image');
$mimeType = $categoryFile->getClientOriginalName();
$path = public_path() . '/storage/category/';
$categoryFile->move($path, $mimeType);
$cat->cat_image = $mimeType;
}
$cat->save();
toastr()->success($cat->name,'Category added!');
return redirect('/category');
}
I have done uploading using controller instead of ajax because there were some complications...So kindly help me with image preview from controller.

refer Below Link
enter link description here
Add This function to your javascript code
function readURL(input) {
if (input.target.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#category-img-tag').attr('src', e.target.result);
}
reader.readAsDataURL(input.target.files[0]);
}
}
$("#cat_image").change(function(){
readURL(this);
});
CHANGE YOUR blade.php file code like <img src="#" id="category-img-tag" width="200px" />
<div class="row">
<label for="image" class="col-sm-2 col-form-label">Category Image</label>
<div class="col-sm-7">
<input id="cat_image" type="file" class="form-control" name="cat_image">
<img src="#" id="category-img-tag" width="200px" /> <!--for preview purpose -->
</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With