尽管通过VUE中的Axios发送图像,但仍会获得图像验证错误
我正在处理laravel 5.8和VUE 2
应用程序,其中我正在通过Axios
发送图像,但我得到文件必须是图像
错误错误图像。
这是我的uploader
:
<div class="row">
<div class="col-md-12 form-group">
<input type="file" name="file" id="file" multiple class="form-control"
@change="setFile">
</div>
</div>
这是我的vue数据对象
:
data(){
return {
form: new FormData,
amount: null,
selectedBmonths: [],
student: null,
bmonths: [],
result: null,
errors: null,
}
},
这是setFile
方法,将图像添加到formdata
:
setFile(event)
{
for(const file of event.target.files)
this.form.append('files[]', file);
}
这是提交表格的subtsform
方法:
submitForm()
{
this.clearErrors();
this.form.append('student_id', this.student.stdId);
this.form.append('batchId', this.student.batchId);
this.form.append('batchfee', this.student.fee);
this.form.append('amount', this.amount);
for (var i = 0; i < this.selectedBmonths.length; i++)
{
this.form.append('selectedBmonths[]', this.selectedBmonths[i]);
}
const config = { headers: {'Content-Type': 'multipart/form-data' }};
axios.post('/university/api/discounts', this.form, config)
.then(response => {
//this.$router.push('/university/discounts/' + response.data.id);
console.log(response.data);
})
.catch(errors => {
this.errors = errors.response.data.errors;
console.log(this.errors);
for(const key in this.form)
{
if(this.errors && this.errors[key] && this.errors[key].length > 0)
{
document.getElementById('err' + key).innerHTML = this.errors[key][0];
}
}
});
},
这是laravel验证
在控制器中的函数:
private function validateData()
{
return request()->validate(
[
'batchId' => 'required|integer',
'batchfee' => 'required|integer',
'amount' => 'required|integer|lte:batchfee',
'student_id' => 'required|integer',
'selectedBmonths' => 'nullable|array',
'files.*' => 'image',
],
[
'amount.lte' => 'Discount should be less than or equal to the batch fee.',
]
);
}
i 提交
form
,我收到以下错误消息:
注意:如您所见,我已经设置了右标头/配置也是,但我仍然会收到图像验证错误消息。
任何帮助都会提前赞赏。
I am working on a laravel 5.8 and vue 2
application where I am sending images through axios
but I get file must be an image
error despite sending image.
Here is my uploader
:
<div class="row">
<div class="col-md-12 form-group">
<input type="file" name="file" id="file" multiple class="form-control"
@change="setFile">
</div>
</div>
Here is my vue data object
:
data(){
return {
form: new FormData,
amount: null,
selectedBmonths: [],
student: null,
bmonths: [],
result: null,
errors: null,
}
},
Here is the setFile
method that adds images to the FormData
:
setFile(event)
{
for(const file of event.target.files)
this.form.append('files[]', file);
}
Here is the submitForm
method that submits the form:
submitForm()
{
this.clearErrors();
this.form.append('student_id', this.student.stdId);
this.form.append('batchId', this.student.batchId);
this.form.append('batchfee', this.student.fee);
this.form.append('amount', this.amount);
for (var i = 0; i < this.selectedBmonths.length; i++)
{
this.form.append('selectedBmonths[]', this.selectedBmonths[i]);
}
const config = { headers: {'Content-Type': 'multipart/form-data' }};
axios.post('/university/api/discounts', this.form, config)
.then(response => {
//this.$router.push('/university/discounts/' + response.data.id);
console.log(response.data);
})
.catch(errors => {
this.errors = errors.response.data.errors;
console.log(this.errors);
for(const key in this.form)
{
if(this.errors && this.errors[key] && this.errors[key].length > 0)
{
document.getElementById('err' + key).innerHTML = this.errors[key][0];
}
}
});
},
Here is the Laravel Validation
function in controller:
private function validateData()
{
return request()->validate(
[
'batchId' => 'required|integer',
'batchfee' => 'required|integer',
'amount' => 'required|integer|lte:batchfee',
'student_id' => 'required|integer',
'selectedBmonths' => 'nullable|array',
'files.*' => 'image',
],
[
'amount.lte' => 'Discount should be less than or equal to the batch fee.',
]
);
}
When I submit
the form
, I get the following error messages:
Note: As you can see, I have set the right headers/config as well but still I get the image validation error messages.
Any help is appreciated in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我准备了与您相同的环境,并进行了测试。我收到相同的错误消息。它仅适用于PNG文件。但是我使用 mime
使用 image 验证,它可以使用。
我的验证:
我的JavaScript(与您的JavaScript几乎相同):
而且,我认为最好将
Accept
属于输入,例如:希望这对您有帮助。
I prepared the same environment as yours and tested it. I got the same error messages. It only works for png files. But I use mime validation instead of
using image validation, it works.
My validation:
My javascript (almost same as yours):
And also, I think it will be better to add
accept
attribute to the input, like:Hope this will help you.