1

I have a component that works on create form, but not on an update form. The image is being shown in the update form, but when I try to upload it, in the backend, I don't get any file for the field 'image'

This is the component:

<template>
<div>
  <div v-if="!image">
    <h2>Select an image</h2>
    <input type="file" @change="onFileChange">
  </div>
  <div v-else>
    <img :src="image" />
    <input type="file" name="image" style="display:none">
    <button @click="removeImage">Remove image</button>
  </div>
</div>
</template>

<script>
export default {
    props: {
        image: {
            type: String,
            default: ""
        }
    },
    data() {
        return {
        formData:new FormData(),
        file: null
        }
    },
    methods: {
        onFileChange: function onFileChange(e) {
            var files = e.target.files || e.dataTransfer.files;
            if (!files.length)
                return;
            this.createImage(files[0]);
            this.formData.append('file', files[0]);
            this.file = files[0];
        },
        createImage: function createImage(file) {
            var image = new Image();
            var reader = new FileReader();
            var vm = this;
            reader.onload = function (e) {
                vm.image = e.target.result;
            };
            reader.readAsDataURL(file);
        },
        removeImage: function removeImage(e) {
            this.image = '';
        }
    }
}
</script>

And this is how I call it in the create view:

<image-upload></image-upload>

And this is for the update view:

<image-upload image="/uploads/{{ $magazine->image }}"></image-upload>

But, when I do dd($request->all()) in the update function in my controller I get this output:

array:8 [▼
  "_method" => "PUT"
  "_token" => "oNkMAyKsKsxOpqeonRDvyusqtFrfVgBEQOnyNrFw"
  "image" => "1.png"
  "name" => "Article name"
  "summary" => ""
  "visual_id" => ""
  "spid_id" => ""
  "files" => "1"
]

And for the create function where it actually works I get:

array:6 [▼
  "_token" => "oNkMAyKsKsxOpqeonRDvyusqtFrfVgBEQOnyNrFw"
  "name" => "Article name"
  "summary" => ""
  "visual_id" => ""
  "spid_id" => ""
  "image" => UploadedFile {#222 ▶}
]

This is the form:

{!! Form::model($magazine, ['method' => 'PUT', 'route' => ['magazines.update', 'id' => $magazine->id, 'files' => true]]) !!}
<div class="row magasin-form large-6 large-offset-3 columns">

    <ul class="tabs">
      <li class="tabs-title is-active"><a href="{{ url('/admin/magazines') }}" aria-selected="true">Tilbake</a></li>
    </ul>

    <div class="tabs-content">
        <div class="tabs-panel is-active">
            @section('errors')
              @include('layouts.partials.errors')
            @show
            <p>Redigere</p>

            <div class="row">
                <div class="large-12 columns">
                    <label>Navn
                        {!! Form::text('name', $magazine->name, ['placeholder' => 'Navn']) !!}
                    </label>
                </div>
            </div>

            <image-upload image="/uploads/{{ $magazine->image }}"></image-upload>

            <div class="row">
                <div class="large-12 columns">
                    <label>Visual ID
                        {!! Form::text('visual_id', $magazine->visual_id, ['placeholder' => 'Visual id']) !!}
                    </label>
                </div>
            </div>

            <div class="row">
                <div class="large-12 columns">
                    <label>Spid ID
                        {!! Form::text('spid_id', $magazine->spid_id, ['placeholder' => 'spid id']) !!}
                    </label>
                </div>
            </div>

            <div class="row">
                <div class="large-12 columns">
                    <label>Summary
                        {!! Form::textarea('summary', $magazine->name) !!}
                    </label>
                </div>
            </div>

            <div class="row">
                <div class="large-6 columns">
                  {!! Form::submit('Lagre', ['class'=> 'secondary button']) !!}
                </div>
                <div class="large-6 columns">
                  <a class="secondary hollow button" href="{{ route('magazines.index')}}">Avbryte</a>
                </div>
            </div>
        </div>
    </div>
</div>
{!! Form::close() !!}

Updated

I have also tried with changing my component to this:

<template>
    <div class="Image-input">
        <div class="Image-input__input-wrapper">
            <h2>+</h2>
            <input @change="previewThumbnail" class="Image-input__input" name="thumbnail" type="file">
        </div>

        <div class="Image-input__image-wrapper">
            <i v-show="! imageSrc" class="icon fa fa-picture-o"></i>
            <img v-show="imageSrc" class="Image-input__image" :src="imageSrc">
        </div>
    </div>
</template>
<script>
export default {

  props: ['imageSrc'],

  methods: {
    previewThumbnail: function(event) {
      var input = event.target;

      if (input.files && input.files[0]) {
        var reader = new FileReader();

        var vm = this;

        reader.onload = function(e) {
          vm.imageSrc = e.target.result;
        }

        reader.readAsDataURL(input.files[0]);
      }
    }
  }
}
</script>

But I get the same output for my form on update.

1
  • Pretty sure it has something to do with you messing with the FormData. Do you use a different form for Create? Commented Oct 20, 2016 at 16:03

2 Answers 2

0
+50

It looks like you're accidentally passing the files boolean to the route array instead. Try updating your form opening to:

{!! Form::model($magazine, ['method' => 'PUT', 'route' => ['magazines.update', 'id' => $magazine->id], 'files' => true]) !!}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I forgot to mention that I already solved it and it was this stupid mistake.
0

Your input field doesn't have a name so that might be why the form won't pick it up

Not sure why you are modifying the FormData. Any Reason you can't just submit the <input> with the form instead of extracting it?

Try showing us how the form looks, the place where you put your image-upload component

1 Comment

I am not sure what do you mean by that my input field doesn't have a name, since I have name="image" in <input type="file" name="image" style="display:none">. I have added just int case name="image" to the input field in v-if="!image" block, but it didn't help. I get the same output. I have also updated the question with the form.

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.