【问题标题】:Save path image with Ajax in Laravel在 Laravel 中使用 Ajax 保存路径图像
【发布时间】:2016-02-13 08:36:53
【问题描述】:

我可以保存表单中的所有数据 varchar/text 元素,但无法保存路径图像。

我的代码有什么问题?

让我们看看我的create.blade.php 我可以保存 var deadline 的值,但我不能保存 var path 的值:

Form::open(array('url' => 'imagesLoker', 'files' => true))
    <form class="form-horizontal">
    <div class="box-body">
        <div class="form-group">
            {!!Form::label('Deadline Lowongan : ')!!}
            {!!Form::date('deadline',null,['id'=>'deadline','class'=>'form-control','placeholder'=>'Deadline Lowongan'])!!}
        </div>
        <div class="form-group">
            {!!Form::label('Image Lowongan : ')!!}
            {!!Form::file('path') !!}
        </div>
    </div><!-- /.box-body -->
</form>
{!!Form::close()!!}

这是我的控制器:

public function store(Request $request)
    {
        Lowongan::create($request->all());
        return "data all";
    }

这是我创建数据的 Ajax:

$("#createLoker").click(function(){
    var datas = $('form').serializeArray();
    var route = "http://localhost:8000/lowongan";
    var token = $("#token").val();
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    $.post(route,{
        deadline: $("#deadline").val(),
        path: $("#path").val()
    }).done(function(result){
        console.log(result);
    });
});

我不知道这对于在我的模态中设置解析数据是否重要,但我只是将这段代码放在我的模态中:

class Lowongan extends Model
{
    protected $table = 'Lowongan';
    protected $fillable = ['path','deadline'];

    public function setPathAttribute($path){
        $this->attributes['path']  = Carbon::now()->second.$path->getClientOriginalName();
        $name = Carbon::now()->second.$path->getClientOriginalName();
        \Storage::disk('local')->put($name, \File::get($path));
    }
}

最后我设置了保存图片的目录。这是config/filesystem中的设置:

'disks' => [
        'local' => [
            'driver' => 'local',
            'root'   => public_path('imagesLoker'),
        ],

我可以保存数据截止日期,但不能保存图像:( ..如果有任何关于如何保存图像路径的想法,我会很高兴知道。

【问题讨论】:

标签: javascript php jquery ajax laravel


【解决方案1】:

在您的表单中,您必须允许 laravel like. 中的文件上传选项

Form::open(array('url' => 'foo/bar', 'files' => true))

查看laravel doc的文件上传部分

希望对你有帮助..

【讨论】:

    【解决方案2】:

    请按照以下步骤操作

    在视图中

    {!!Form::file('path') !!} 更改为{!!Form::file('file') !!}

    在控制器中

    请注意我已将上传路径设置为root/public/uploads/ 文件夹

    public function store(Request $request)
        {
            $file = Input::file('file');
            $path = '/uploads/';
    
            $newFileName = Carbon::now()->second.$file->getClientOriginalName(). '.' . $file->getClientOriginalExtension();
    
            // Move the uploaded file
            $upSuccess = $file->move(public_path() . $path, $newFileName);
    
            $result = file_exists(public_path() . $path . $newFileName);
    
            $fileData =[
                'fileType' => $file->getClientOriginalExtension(),
                'filePath' => substr($path, 1) . $newFileName
            ];
    
            Input::merge(array('path' => $fileData["filePath"]));
    
            Lowongan::create($request->all());
            return "data all";
        }
    

    【讨论】:

      猜你喜欢
      • 2020-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-11
      • 1970-01-01
      • 2020-01-25
      • 2011-06-20
      • 2015-07-14
      相关资源
      最近更新 更多