laravel - Laravel 验证文件扩展名?
问题描述
我有以下 FormRequest 验证Files
,特别是音频文件:
public function rules()
{
return [
'file' => 'bail|required|file|mimes:mpeg,mp3,mp2,aiff,wav,wave,flac,m4a,caf,ogg,aac,amr,wma|max:500000000'
];
}
但是,由于某种原因,当我上传任何类型的音频文件时,我会收到一条422
说明文件类型无效的消息……这是什么原因?
更新
这是我的ajax
使用axios
:
const f = new FormData()
f.append('file', file)
const config = {
headers: {
'Content-Type':'multipart/form-data'
}
}
return axios.post('myendpoint', f, config).then((response) => {
// handle
}).catch((thrown) => {
if (axios.isCancel(thrown)) {
return console.log('Request canceled', thrown.message);
}
return console.log(thrown)
})
解决方案
你的表格中有这个:
enctype=“multipart/form-data"
编辑:为了正确地将您上传的文件附加到 FormData,您需要在以下位置使用onchange
事件:
输入
<input type=“file” onchange=“getFile”>
处理程序
function getFile(event) {
file = event.target.files
}
阅读此处了解更多信息。
推荐阅读
- amazon-web-services - 我可以将托管在 EC2 中的其余服务用作 SNS HTTP 订阅吗?
- python - Arkham 起源于 Python?
- c++ - 使用 remove_if 从向量中删除元素
- messagebroker - IIB(IBM Integration Bus)中的会计和统计
- nginx - Helm - 通过 Nginx Ingress 访问 Grafana
- java - Spring Boot应用程序:http响应时间的负值
- java - 在 Cloud Firestore (Android) 上使用数组字段收听某些查询的更新
- java - 如何使用 java 将巨大的数组数据加载到 snappydata 中?
- reactjs - ReactJS 查询变量
- mysql - 通过子帖子内容显示父帖子