Laravel如何处理文件上传:Can';不保存图像文件名
所以我想上传一张图片,但它只正确地存储了它的描述。该文件存储为“noimage.jpg”,这意味着文件名不被读取。我正在使用modal btw。我的数据库名为库,迁移包含以下内容:Laravel如何处理文件上传:Can';不保存图像文件名,laravel,file-upload,controller,store,laravel-7,Laravel,File Upload,Controller,Store,Laravel 7,所以我想上传一张图片,但它只正确地存储了它的描述。该文件存储为“noimage.jpg”,这意味着文件名不被读取。我正在使用modal btw。我的数据库名为库,迁移包含以下内容: $table->id() $table->timestamps() $table->string('upload') $table->longtext('description')->nullable() 控制器: public function store(Request $request) {
- $table->id()李>
- $table->timestamps()李>
- $table->string('upload')李>
- $table->longtext('description')->nullable()李>
public function store(Request $request)
{
$galleries=new Gallery;
// Handle File Upload
if($request->hasFile('upload')){
// Get filename with the extension
$filenameWithExt = $request->file('upload')->getClientOriginalName();
// Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
// Get just ext
$extension = $request->file('upload')->getClientOriginalExtension();
// Filename to store
$fileNameToStore= $filename.$extension;
// Upload Image
$path = $request->upload('upload')->storeAs('public/upload', $fileNameToStore);
// make thumbnails
$thumbStore = 'thumb.'.$filename.'_'.time().'.'.$extension;
$thumb = Image::make($request->file('upload')->getRealPath());
$thumb->save('storage/upload/'.$thumbStore);
} else {
$fileNameToStore = 'noimage.jpg';
}
$galleries->description = $request->input('description');
$galleries->upload = $fileNameToStore;
$galleries->save();
}
表格:
选择文件
选择文件
上传的图像将在下面的框中呈现
说明文字
阿贾克斯
$(文档).ready(函数(){
$.ajaxSetup({
标题:{
'X-CSRF-TOKEN':$('meta[name=“CSRF-TOKEN”]).attr('content'))
}
});
//添加图片
$('#btnUpload')。单击(函数(){
$('#uploadModal').modal('show');
});
$('#btnSave')。单击(函数(){
$.ajax({
数据:$('#uploadForm')。序列化(),
url:“{route('home.store')}}”,
类型:“POST”,
数据类型:“json”,
成功:功能(数据){
$('#uploadModal').modal('hide');
},
错误:函数(数据){
console.log('错误:',数据);
}
});
});
//图像读取器在选中时显示pic
函数readURL(输入){
if(input.files&&input.files[0]){
var reader=new FileReader();
reader.onload=函数(e){
$(“#imageResult”)
.attr('src',e.target.result);
};
reader.readAsDataURL(input.files[0]);
}
}
$(函数(){
$('#upload')。on('change',function(){
读取URL(输入);
});
});
var input=document.getElementById('upload');
var infoArea=document.getElementById('upload label');
addEventListener('change',showFileName);
函数showFileName(事件){
var输入=event.src元素;
var fileName=input.files[0]。名称;
infoArea.textContent='文件名:'+文件名;
}
});
您不需要在表单标签中添加“enctype”
更改您的代码:
<form id="uploadForm">
<form id="uploadForm" enctype='multipart/form-data'>
与:
<form id="uploadForm">
<form id="uploadForm" enctype='multipart/form-data'>
如果上述操作不起作用,则: 1.通过以下命令安装此软件包:
composer require "laravelcollective/html":"^5.2.0"
2.在config/app中添加此行:
'providers' => [
// ...
**Collective\Html\HtmlServiceProvider::class,**
// ...
],
and
'aliases' => [
// ...
**'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,**
// ...
],
3.更改表格:
{!! Form::open([ 'action'=> 'NameofController@store', 'method' => 'POST','enctype' => 'multipart/form-data' ]) !!}
<div class="form-group">
{{Form::text('name','' , ['class' =>'form-control'])}}
</div>
<div class="form-group">
{{Form::text('phone','' , ['class' =>'form-control', 'placeholder' => 'phone' ])}}
</div>
<div class="form-group">
{{Form::file('image_name')}}
</div>
{{Form::submit('Submit' , ['class' =>'btn btn-primary' ])}}
{!! Form::close() !!}
{!!表单::打开(['action'=>'NameofController@store“,”方法“=>”POST“,”enctype“=>”多部分/表单数据“])
{{Form::text('name','',['class'=>'Form-control'])}
{{Form::text('phone','',['class'=>'Form-control','placeholder'=>'phone'])}
{{Form::file('image_name')}
{{Form::submit('submit',['class'=>'btn-btn-primary'])}
{!!Form::close()!!}
您可以通过以下方式使用自定义名称存储文件:
$path = $request->file('upload')->store('public/upload/' .$fileNameToStore);
假设使用ajax上载文件,则需要使用FormData对象,而不是
.serialize()
将enctype='multipart/form data'添加为
未更改的文件名仍保存为“noimage.jpg”欢迎使用stackoverflow,尝试执行dd($request->hasFile('upload')代码>&检查您是否得到了正确答案,或者您可以只显示dd($request)代码>在这个问题中,我得到了一个很长的回复文本:ƒ(obj)这是它的开头:Sfdump=window.Sfdump | |(function(doc){var refStyle=doc.createElement(…
尝试查看您在控制器中的$request中得到了什么。更改后的文件名仍然保存为“noimage.jpg”很抱歉,我们还需要不使用这种表单格式,只使用经典的php和html样式…呵呵,但是这种方法是安全的。我们的这位教授希望我们如此伤心,不能使用它。
$.ajax({
data: new FormData($('#uploadForm').get(0)), // use formdata object
url: "{{ route('home.store') }}",
type: "POST",
dataType: 'json',
contentType: false, // required for
processData: false, // jquery ajax file upload
success: function(data){
$('#uploadModal').modal('hide');
},
error: function(data){
console.log('Error:', data);
}
});