【发布时间】: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'),
],
我可以保存数据截止日期,但不能保存图像:( ..如果有任何关于如何保存图像路径的想法,我会很高兴知道。
【问题讨论】:
-
您不能使用 $.post 上传带有 ajax 的图像。看看这个stackoverflow.com/questions/11046684/…
标签: javascript php jquery ajax laravel