为什么更新图片没有检测到上传的文件?

问题描述 投票:5回答:1

我正在使用laravel 5为配置文件创建编辑表单,并可以更新表单中的图片。

我想以编辑形式存储新图像。我在edit.blade中使用此代码来获取用户的图像。

视图:

{!! Form::model($dataItemregistration,['method' => 'PATCH', 'action' => ['Modul\ProfilController@update', $dataItemregistration->ItemRegistrationID, 'files' => true] ]) !!}
<div class="form-group">
    <div class="row">
        <div class="col-lg-3"> 
            {{ Form::label('pic', 'Gambar (Saiz gambar, 250x300px)') }}
        </div>
        <div class="col-lg-7">
            {!! Form::file('gambar', array('class' => 'form-control')) !!}
        </div>
    </div>
</div>
<br>
<div class="col-lg-10 text-center">
   {!! link_to(URL::previous(),'Back', ['class' => 'btn btn-warning btn-md']) !!}
   {{ Form::submit('Update', ['class' => 'btn btn-primary']) }}
</div>
{!! Form::close() !!}

控制器:

public function update(Request $request, $id)
{
    $valueitemregistrations = Itemregistration::find($id);

    $this->validate($request,['gambar' => 'max:100000',]);
    if ($request->hasFile('gambar')) {
        // Get the file from the request
        $file = $request->file('gambar');
        // Get the contents of the file
        $content = $file->openFile()->fread($file->getSize());

        $valueitemregistrations->Picture = $content;
        $valueitemregistrations->update();

        if($valueitemregistrations) {
            return redirect('profil');
        } else {
            return redirect()->back()->withInput();
        }
    } else { 
        echo "testing";
    }
}

当我尝试上传和更新时,它会回显“测试”。它没有检测到任何上传的文件..

我一直在使用相同的代码add.blade,它的工作原理。

它与路径路径有关吗?

laravel laravel-5 file-upload image-uploading
1个回答
4
投票

当您的HTML表单没有enctype="multipart/form-data"时会发生这种情况。

在这种情况下,原因是'files' => trueForm::model()内部错误数组的一部分;当它应该在外面时它在'action'阵列里面。试试这个:

Form::model($dataItemregistration, [
    'method' => 'PATCH',
    'action' => ['Modul\ProfilController@update', $dataItemregistration->ItemRegistrationID],
    'files' => true,
]);
© www.soinside.com 2019 - 2024. All rights reserved.