Laravel验证器和Excel文件错误 [英] Laravel validator and excel files error
问题描述
我有一个输入字段,允许人们上载文件. 我希望他们可以上载Word文件(例如doc)和csv,xlsx这样的文件.
I have an input field who allaow peoples to upload files. I want that they can upload, word files like doc, and files like csv,xlsx.
当我尝试使用.doc时,没有任何问题,但是当我尝试使用excel文件时,验证程序失败,并说不是很好的扩展名.
When i try with a .doc no problem at all but when i try with an excel files, the validator fail and say that not the good extension.
在这里您可以看到我的代码,两行注释是我尝试过的另一种解决方案,它也不起作用:(.
Here you can see my code, the two lines of comments was an other solution i have try , and it don't work too :(.
欢迎任何帮助.
public function postFile(Request $request)
{ //Règle de validation avec les type de fichiers acceptés
if(isset($request->file)){
//dd($request);
$validator=Validator::make($request->all(),[
'file'=>'required|max:50000|mimes:xlsx,doc,docx,ppt,pptx,ods,odt,odp,application/csv,application/excel,
application/vnd.ms-excel, application/vnd.msexcel,
text/csv, text/anytext, text/plain, text/x-c,
text/comma-separated-values,
inode/x-empty,
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
/* 'extension' => strtolower($request->file->getClientOriginalExtension()),
'extension'=>'required|in:doc,csv,xlsx,xls,docx,ppt,odt,ods,odp'*/
]);
if ($validator->fails()) {
return back()
->withErrors($validator);
}
推荐答案
好的,我的错. 我尝试了另一种解决方案,该解决方案可以在该网站上找到并且有效. 感谢您的帮助Odin. 这是我在这个网站上的第一个问题.我要看看我现在是否可以帮助某人. 我为有需要的人发布了解决方案的代码:).
Ok, my fault. I had tried another solution, found on this website and it worked. Thanks for help Odin. It was my first question on this website. I am gonna see if I can help someone now. I post code for solution for someone in need :).
$validator = Validator::make(
[
'file' => $request->file,
'extension' => strtolower($request->file->getClientOriginalExtension()),
],
[
'file' => 'required',
'extension' => 'required|in:doc,csv,xlsx,xls,docx,ppt,odt,ods,odp',
]
);
这篇关于Laravel验证器和Excel文件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!