使用Laravel Form Facade上传图片的表单
{{Form:: open(['route'=>'jcrop.store', 'method'=>'post','id'=>'upload','role'=>'form-role', 'enctype'=>'multipart/form-data'])}}
@include('backend.admin.jcrop.general._form',['submitButton'=>'Save Values'])
{{Form:: close()}}
包含的表格...
<div class="form-group">
{{Form:: label('title','Title')}}
{{Form:: text('title',null,['placeholder'=>'Enter Title', 'class'=>'form-control'])}}
</div>
<div class="form-group">
{{Form:: label('rank','Rank')}}
{{Form:: number('rank',null,['placeholder'=>'Enter Rank', 'class'=>'form-control'])}}
</div>
<div class="form-group">
{{Form:: label('image','Image')}}
{{Form:: file('image',null,['placeholder'=>'Enter Rank', 'class'=>'form-control', 'id'=> 'image'])}}
</div>
<div class="form-group">
{{Form:: label('status','Status:')}}
{!! Form::radio('status', '1', 'true')!!} Active
{!! Form::radio('status', '0')!!} De-Active
</div>
{{Form:: button($submitButton,['class'=>'btn btn-primary','type'=>'submit'])}}
{{Form:: button('Reset',['class'=>'btn btn-danger','name'=>'reset','type'=>'reset'])}}
使用 malsupform 插件上传图片的脚本。
<script type="text/javascript">
$("document").ready(function(){
var options= {
beforeSubmit: showRequest,
success: showResponse,
dataType: 'json'
};
$("#image").change(function(){
$("#upload").ajaxForm(options).submit();
});
});
function showRequest(formData, jqForm, options){
$("#validation-errors").hide().empty();
return true;
}
function showResponse(response, statusText, xhr, $form){
if(response.success==false)
{
var arr= response.errors;
$.each(arr, function(index, value){
if(value.length=!0){
$("#validation-errors").append('<div class="alert alert-danger">+value+</div');
}
});
$("#validation-errors").show();
}else{
console.log(response.file);
$("#output").html("<img src='"+response.file+"'/>");
}
}
</script>
处理上传和存储方法的路由。
Route::resource('jcrop', 'Admin\JcropController');
处理上传并返回数据的 json 格式的 Controller 。
这是 Controller 文件,我在其中声明了一些 protected 变量。
protected $view_path = 'backend.admin.jcrop';
protected $imagePath = '\public\uploads\jcrop';
protected $imageUrl = 'uploads\jcrop';
处理上传和返回数据的 json 格式的 Controller 函数..
public function store(Request $request)
{
if($request->hasFile('image')){
$image= $this->saveImage($request->file('image'));
这里我做了一个简单的函数,叫做saveImage(),它上传图片并返回上传的文件
$file= base_path(). $this->imagePath.'/' .$image->getFileName();
if($image){
return response()->json(['success'=>'true', 'file'=>$file]);
}
}
}
处理上传的函数是...
protected function saveImage($image)
{
$filename= $image->getClientOriginalName();
$filename= pathinfo($filename, PATHINFO_FILENAME);
$file= $filename. '.' .$image->getClientOriginalExtension();
$upload= $image->move(base_path().$this->imagePath, $file);
return $upload;
}
一切正常,除了我看到返回数据时
Not allowed to load local resource:
在 chrome 和 FireFox 中也是如此。请帮我解决这个问题?
最佳答案
我认为问题在于 saveImage()
返回。尝试使用 url('/')
而不是 base_path()
。 base_path()
返回服务器路径而不是 URL。
$file= url('public/uploads/jcrop').$image->getFileName();
关于javascript - Laravel Ajax 图片上传预览不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40701853/