简体   繁体   English

laravel5.8 上传头像前的图片预览

[英]laravel5.8 Image preview before uploading a profile photo

I have profile image upload section.我有个人资料图片上传部分。 I want to preview an image before uploading.我想在上传之前预览图像。 But I got an error saying但我得到一个错误说

Too few arguments to function arguments 到 function 太少

I have already executed php artisan storage:link but I can not store my image in uploads/profile/ folder.我已经执行了 php artisan storage:link但我无法将图像存储在 uploads/profile/ 文件夹中。 So I cannot insert image data in mysql server.所以我无法在 mysql 服务器中插入图像数据。

I want to preview an image before uploading, and submit all the data by the other submit button.我想在上传之前预览图像,并通过另一个提交按钮提交所有数据。

Here is what I tried.这是我尝试过的。

UserController.php UserController.php

public function store(Request $request) {
    $this->validate($request,[
        'image'=>'image|mimes:png, jpg, jpeg|max:20000',
        'name'=>'required',
        'gender'=>'required',
        'bod'=>'required|before:today'
    ]);
    $user_id = auth()->user()->id;
    Profile::updateOrCreate(
        ['user_id' => $user_id], // search requirements
        [   'image' => request('image'),
            'name' => request('name'),
            'gender' => request('gender'),
            'country' => request('country'),
            'bod' => request('bod'),
            'description' => request('description')
        ]
    );
    return redirect()->route('profile.index');
}

public function upload(Request $request) {

        $this->validate($request,[
                'image'=>'image|mimes:png, jpg, jpeg|max:20000'

        $user_id = auth()->user()->id;
        if($request->hasfile('image')){
            $file = $request->file('image');
            $ext = $file->getClientOriginalExtension();
            $filename = time().'.'.$ext;
            $file->move('uploads/profile/', $filename);
            Profile::where('user_id',$user_id)->update([
                'image'=>$filename
            ]);
        }
        return redirect()->back();
    }

web.php web.php

Route::prefix('user')->group(function(){
Route::resource('profile', 'UserController');
Route::resource('profile', 'UserController@upload')->name('profile.upload');

}); }); create.blade.php create.blade.php

<div class="image-preview" id="imagePreview">
         @if(empty(Auth::user()->profile->image))
              <img src="{{asset('image/image1.jpg')}}" class="image-preview__image">
          @else
       <img src="{{asset('uploads/profile')}}/{{Auth::user()->profile->image}}" class="image-preview__image">
                            @endif
                    </div>


      <form action="{{ route('profile.upload') }}" method="POST" enctype="multipart/form-data">
    @csrf
    <div class="btnUpload">
              <input type="file" name="image" id="inpFile"hidden="hidden">
     </div>

      <div class="edit-image">
           <button type="submit" class="editBtn" id="custom-button">Change Image</button>
       </div>
   </form>


<form action="{{ route('profile.store') }}" method="POST" >
  @csrf
//other profile info
 <button type="submit" class="saveBtn">Save</button>
                </div>
            </form>

JavaScript JavaScript

const inpFile = document.getElementById("inpFile");
const previewContainer = document.getElementById("imagePreview");
const previewImage = previewContainer.querySelector(".image-preview__image");

inpFile.addEventListener("change", function() {
    const file = this.files[0];
    if(file) {
        const reader = new FileReader();

        reader.addEventListener("load", function(){
            previewImage.setAttribute("src", this.result);
        });

        inpFile.style.display = "none";
        previewImage.style.display = "block";

        reader.readAsDataURL(file);
    } else {
        inpFile.style.display = null;
        previewImage.style.display = null;
        previewImage.setAttribute("src", "");
    }

});

const realFileBtn = document.getElementById("inpFile");
const customBtn = document.getElementById("custom-button");

    customBtn.addEventListener("click", function(){
        realFileBtn.click()
    });

When I don't use form method, JavaScript is working good.当我不使用表单方法时,JavaScript 运行良好。

You don't need that much script to display an image after selecting.选择后您不需要那么多脚本来显示图像。 There is a simple way out.有一个简单的出路。 Here is an example:这是一个例子:

 <input type="file" name="exmaple" id="inpFile" onchange="$('.image-preview__image')[0].src = window.URL.createObjectURL(this.files[0])"  required>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM