【发布时间】:2020-08-11 02:54:33
【问题描述】:
我喜欢一次上传多个文件。
前端 Vue / Vuetify:
<v-file-input
v-model="files"
id="files"
ref="files"
multiple
label="Bild oder Datei hinzufügen"
@change="handleFilesUpload"
>
export default {
name: "HeaderNotesModal",
data: () => ({
media: [],
files: [],
}),
methods: {
submit () {
this.$refs.form.validate()
if (this.valid) {
this.store({
payload: {
title: this.title,
body: this.body
},
context: this
}).then(() => {
if (this.errors.length === 0) {
this.title = ''
this.body = ''
this.$refs.form.resetValidation()
this.form = false
}
if( this.files.length !== 0 ){
this.addMedia({ 'media': this.formData})
}
})
}
},
handleFilesUpload(e){
var self = this;
var files = this.files;
if(files.length > 0){
for(var i = 0; i< files.length; i++){
self.formData.append("file[]", files[i], files[i].name);
}
}
},
},
mounted() {
this.fetchNotes()
}
}
export const addMedia = ({ state, commit }, { media}) => {
return axios.post(`/api/notes/${state.note.id}/media`, media, {
headers: {
'Content-Type': 'multipart/form-data'
}
})
}
后端 Laravel:
public function store(Request $request, Note $note)
{
if($request->hasfile('file'))
{
foreach($request->file('file') as $file)
{
$name = time().'.'.$file->getClientOriginalExtension();
$file->move(storage_path('uploads'), $name);
$data[] = $name;
}
}
return 'success';
}
当我上传多个文件时,它只存储最后一个文件。
我尝试了几种可能的方法,但没有发现错误。
我尝试更改前端,然后也是后端。
我尝试了使用 formData 和没有。
我尝试了几个文件名。
都没有成功
var_dump return var_dump($request->file('file'));
<pre class='xdebug-var-dump' dir='ltr'>
<small>/home/vagrant/code/7aio/app/Http/Controllers/Note/NoteMediaController.php:27:</small>
<b>array</b> <i>(size=2)</i>
0 <font color='#888a85'>=></font>
<b>object</b>(<i>Illuminate\Http\UploadedFile</i>)[<i>392</i>]
<i>private</i> 'test' <small>(Symfony\Component\HttpFoundation\File\UploadedFile)</small> <font color='#888a85'>=></font> <small>boolean</small> <font color='#75507b'>false</font>
<i>private</i> 'originalName' <small>(Symfony\Component\HttpFoundation\File\UploadedFile)</small> <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'bgvqm7da.png'</font> <i>(length=12)</i>
<i>private</i> 'mimeType' <small>(Symfony\Component\HttpFoundation\File\UploadedFile)</small> <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'image/png'</font> <i>(length=9)</i>
<i>private</i> 'error' <small>(Symfony\Component\HttpFoundation\File\UploadedFile)</small> <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>0</font>
<i>protected</i> 'hashName' <font color='#888a85'>=></font> <font color='#3465a4'>null</font>
<i>private</i> 'pathName' <small>(SplFileInfo)</small> <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'/tmp/phpIIfLwD'</font> <i>(length=14)</i>
<i>private</i> 'fileName' <small>(SplFileInfo)</small> <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'phpIIfLwD'</font> <i>(length=9)</i>
1 <font color='#888a85'>=></font>
<b>object</b>(<i>Illuminate\Http\UploadedFile</i>)[<i>372</i>]
<i>private</i> 'test' <small>(Symfony\Component\HttpFoundation\File\UploadedFile)</small> <font color='#888a85'>=></font> <small>boolean</small> <font color='#75507b'>false</font>
<i>private</i> 'originalName' <small>(Symfony\Component\HttpFoundation\File\UploadedFile)</small> <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'Screenshot at Apr 27 07-35-08.png'</font> <i>(length=33)</i>
<i>private</i> 'mimeType' <small>(Symfony\Component\HttpFoundation\File\UploadedFile)</small> <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'image/png'</font> <i>(length=9)</i>
<i>private</i> 'error' <small>(Symfony\Component\HttpFoundation\File\UploadedFile)</small> <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>0</font>
<i>protected</i> 'hashName' <font color='#888a85'>=></font> <font color='#3465a4'>null</font>
<i>private</i> 'pathName' <small>(SplFileInfo)</small> <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'/tmp/phpdRhiD1'</font> <i>(length=14)</i>
<i>private</i> 'fileName' <small>(SplFileInfo)</small> <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'phpdRhiD1'</font> <i>(length=9)</i>
</pre>
【问题讨论】:
-
您是否检查过
$request->file('file')包含所有文件?我不确定您是否使用self.formData.append("file", files[i], files[i].name);而不是self.formData.append("file[]", files[i], files[i].name);覆盖文件,将"file"更改为"file[]" -
我修改了你写的代码。我编辑了这个问题。您可以看到 var_dump。请求中有两个文件,但只存储了一个。
-
您能否转储并验证为文件生成的名称是否唯一,取决于脚本执行的速度,它可能会为
time()创建相同的值。
标签: laravel vue.js vuetify.js