我有一個組件可以在創建表單上工作,但不能在更新表單上工作。圖像被顯示在更新的形式,但是當我試圖把它上傳,在後臺,我沒有得到任何文件領域'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>
但我得到相同的輸出我的更新表單。
很確定它與你搞亂FormData有關。你是否使用不同的表單創建? – Pitchinnate