我有一个适用于创建表单的组件,但不适用于更新表单。图片正在更新表单中显示,但当我尝试上传时,在后端,我无法获取该字段的任何文件'image'
这是组件:
<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 :(得分:0)
您的输入字段没有名称,这可能就是表单无法提取的原因
不确定为什么要修改FormData。您有什么理由可以只提交表单<input>
而不是提取表单吗?
尝试向我们展示表单的外观,以及放置图片上传组件的位置
答案 1 :(得分:0)
看起来您不小心将files
布尔值传递给route
数组。尝试将表单更新为:
{!! Form::model($magazine, ['method' => 'PUT', 'route' => ['magazines.update', 'id' => $magazine->id], 'files' => true]) !!}