【问题标题】:What is the best practice to implement update profile picture in PHP Laravel 5?在 PHP Laravel 5 中实现更新个人资料图片的最佳实践是什么?
【发布时间】:2015-06-15 12:37:48
【问题描述】:

我试图让我的用户更新他们的个人资料照片,我想知道在 Laravel 中实现类似功能的最佳做法是什么。

这是我的用户个人资料图片。当他们将鼠标悬停在上面时,他们将可以选择更新他们的照片。

当用户点击相机标志或更新头像时,我想带这个文件上传窗口。

不幸的是,我可以做到这一点。


我试过了

{!! Form::model($user, array('route' => array('user.update_profile_picture', $user->id ),'method' => 'PUT')) !!}

<a class="pmop-edit">
  <i class="md md-camera-alt"></i>
  <span class="hidden-xs">Update Profile Picture</span>
</a>

{!! Form::hidden('$id', $user->id)!!}
{!! Form::close()!!}

目前我不确定下一步该做什么。一点点推动将不胜感激。

有人可以对此有所了解吗?

提前致谢。

【问题讨论】:

    标签: javascript php jquery laravel laravel-5


    【解决方案1】:

    我不确定这是否是最佳实践,但您可以尝试使用 Laravel 表单构建器功能

    {!! Form::file('image', null) !!}
    

    您还需要将以下内容添加到您的 Form::open 数组 'files' =&gt; true

    这个页面也可以帮助Processing File Uploads With Laravel 5

    我没有测试过代码,但我做了类似的事情。

    希望这会有所帮助。

    【讨论】:

    • 我已经知道 Laravel 中的 form::file 了。不过还是谢谢你。
    【解决方案2】:

    我通过这样做解决了它

    HTML

    {!! Form::model($user, array('route' => array('user.update_profile_picture', $user->id ),'method' => 'PUT')) !!}
    
    <a class="pmop-edit" >
    <span id="file-upload" >
      <i class="md md-camera-alt"></i>
      <span class="hidden-xs">Update Profile Picture</span>
    </span>
      <div style='height: 0px;width: 0px; overflow:hidden;'>
        <input id="upfile" type="file" value="upload" onchange="sub(this)" />
      </div>
    </a>
    
    {!! Form::hidden('$id', $user->id)!!}
    {!! Form::close()!!}
    

    JS

    $(document).ready(function(){
    
        $("#file-upload").on("click", getFile);
        $("#upfile").on("change", sub);
    
        function getFile() {
            document.getElementById("upfile").click();
        }
    
        function sub(obj) {
            var file = obj.value;
            var fileName = file.split("\\");
            document.getElementById("file-upload").innerHTML = fileName[fileName.length - 1];
            document.myForm.submit();
            event.preventDefault();
        }
    
    });
    

    希望这对像我这样的人有所帮助。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多