laravel7 图片上传正常,但 ajax 说有错误
laravel7 working image upload but ajax says there's error
所以我正在通过模式和 ajax 上传图片。它正在工作,它被保存在数据库中并作为图像保存在 public 文件夹中,除了模态没有隐藏,因为控制台中说的有问题。
statusCode: ƒ ( map )
statusText: "OK"
AJAX:
$(document).ready(function(){
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
//ADD PICTURE
$('#btnUpload').click(function(){
$('#uploadModal').modal('show');
});
$('#btnSave').click(function(){
$.ajax({
data: new FormData($('#uploadForm').get(0)),
url: "{{ route('gallery.store') }}",
type: "POST",
dataType: 'json',
contentType: false, // required for
processData: false, // jquery ajax file upload
success: function(data){
$successmessage = 'SUCCESSFULLY UPLOADED';
$('#uploadModal').modal('hide');
$('#successmessage').text($successmessage);
},
error: function(data){
console.log('Error:', data);
}
});
});
});
控制器:
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->file('upload')->storeAs('public/upload', $fileNameToStore);
} else {
$fileNameToStore = 'noimage.jpg';
}
$galleries->description = $request->input('description');
$galleries->upload = $fileNameToStore;
$galleries->save();
}
在store
函数中,你必须return响应状态码。在成功的情况下,你return200
.
return response()->json(['success' => 'success'], 200);
如果出现故障,您return 对应错误的代码。
示例:
return response()->json(['error' => 'invalid'], 401);
所以我正在通过模式和 ajax 上传图片。它正在工作,它被保存在数据库中并作为图像保存在 public 文件夹中,除了模态没有隐藏,因为控制台中说的有问题。
statusCode: ƒ ( map )
statusText: "OK"
AJAX:
$(document).ready(function(){
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
//ADD PICTURE
$('#btnUpload').click(function(){
$('#uploadModal').modal('show');
});
$('#btnSave').click(function(){
$.ajax({
data: new FormData($('#uploadForm').get(0)),
url: "{{ route('gallery.store') }}",
type: "POST",
dataType: 'json',
contentType: false, // required for
processData: false, // jquery ajax file upload
success: function(data){
$successmessage = 'SUCCESSFULLY UPLOADED';
$('#uploadModal').modal('hide');
$('#successmessage').text($successmessage);
},
error: function(data){
console.log('Error:', data);
}
});
});
});
控制器:
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->file('upload')->storeAs('public/upload', $fileNameToStore);
} else {
$fileNameToStore = 'noimage.jpg';
}
$galleries->description = $request->input('description');
$galleries->upload = $fileNameToStore;
$galleries->save();
}
在store
函数中,你必须return响应状态码。在成功的情况下,你return200
.
return response()->json(['success' => 'success'], 200);
如果出现故障,您return 对应错误的代码。
示例:
return response()->json(['error' => 'invalid'], 401);