2016-10-15 60 views
1

我有一個組件可以在創建表單上工作,但不能在更新表單上工作。圖像被顯示在更新的形式,但是當我試圖把它上傳,在後臺,我沒有得到任何文件領域'image'Vue/Laravel - 在更新上傳文件不起作用

這是組件:

<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> 

而且這就是我把它在創建視圖:

<image-upload></image-upload> 

這是爲更新視圖:

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

但是,當我在我的控制器的更新功能dd($request->all())我得到這樣的輸出:

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

而且對於創建功能,其中它的實際工作,我得到:

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

這是形式:

{!! 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() !!} 

更新

我也曾嘗試與改變我的組件添加到這個:

<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> 

但我得到相同的輸出我的更新表單。

+0

很確定它與你搞亂FormData有關。你是否使用不同的表單創建? – Pitchinnate

回答

0

它看起來像你不小心傳遞files boolean值以route陣列代替。嘗試更新您的表格開放到:

{!! Form::model($magazine, ['method' => 'PUT', 'route' => ['magazines.update', 'id' => $magazine->id], 'files' => true]) !!} 
+0

是的,我忘了提及我已經解決了它,這是一個愚蠢的錯誤。 – Marco

0

你輸入字段沒有命名,那麼這可能是爲什麼形式不會把它撿起來

不知道爲什麼要修改的FORMDATA。任何你不能提交<input>的表單而不是提取它的原因?

嘗試向我們展示的形式看起來如何,你把你的圖像上傳組件的地方

+0

我不知道你是什麼意思,我的輸入字段沒有名稱,因爲我在''。我已經在'v-if =「!image」'塊的輸入字段中添加了int case'name =「image」',但它沒有幫助。我得到相同的輸出。我也用表格更新了這個問題。 – Marco