2

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.

enter image description here

2
  • Try upload image via js, without submitting form. Commented Dec 31, 2019 at 6:40
  • the js part of image upload has a lot of complication in mine thats why i go for controller Commented Dec 31, 2019 at 6:47

1 Answer 1

8

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>
Sign up to request clarification or add additional context in comments.

2 Comments

please mark as verified this answer if its help for you Thank you @kuku
input.target.files[0] is undefined, you need to remove the target so it must be input.files[0] only

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.