Pan*_*kaj 5 laravel vue.js laravel-5.7 laravel-5.8
关于
我正在使用vue.js和Laravel 5.8用纯文本提交图像。
错误详情
当我使用axios提交数据时,它会显示验证错误消息,要求提供产品名称。我同时提交名称和图片。当我禁用代码提交图像时,一切正常。
请求标题-请单击该图像以清楚地查看更多详细信息
有效负载-请单击图像以清楚地查看更多详细信息
HTML
<template>
<div>
<input name="Product Name" type="text" v-model="saveForm.product_name">
<input type="file" accept="image/*" name="product_image" />
<button type="button" @click="store()">Save</button>
</div>
</template>
Run Code Online (Sandbox Code Playgroud)
脚本
<script>
export default {
data() {
return {
saveForm: {
product_name: '', product_image: null
}
};
},
methods: {
store() {
var config = {
headers : {
'Content-Type': 'multipart/form-data', 'processData': false
}
};
var fileData = new FormData();
fileData.append("product_image", this.saveForm.product_image);
fileData.append("product_name", this.saveForm.product_name);
axios.post("my route", this.saveForm, config).then(response => {
if(response.data.Status) {}
})
.catch(error => { //console.log(error);
});
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
Laravel控制器动作方法
public function Create(CreateProductRequest $request) {
//Code here
}
Run Code Online (Sandbox Code Playgroud)
Laravel Request类
class CreateProductRequest extends Request
{
public function wantsJson() {
return true;
}
public function rules() {
return [
'product_name' => 'required',
'product_image' => "image|mimes:bmp,png,jpg,gif,jpeg"
];
}
}
Run Code Online (Sandbox Code Playgroud)
小智 4
好的,让我们逐步检查您的代码。
1)您需要在标题中添加“边界”。它虽然不重要,但对于服务器来说是必需的。
headers: {
"Content-type":
"multipart/form-data; charset=utf-8; boundary=" +
Math.random()
.toString()
.substr(2),
processData: false,
Accept: "application/json"
}
Run Code Online (Sandbox Code Playgroud)
2)为什么通过“new FormData()”准备数据,却用“this.saveForm”发送?正确代码:
axios.post("my route", fileData, config)
.then(response => {
if (response.data.Status) {}
})
.catch(error => { //console.log(error);
});
Run Code Online (Sandbox Code Playgroud)
3)当你按照我上面说的做所有事情时,你会得到一个图像错误,因为你没有通过它。我添加了发送图像的功能。
概括:
网页
<div>
<input
name="Product Name"
type="text"
v-model="saveForm.product_name"
>
<input
type="file"
accept="image/*"
name="product_image"
@change="uploadImage"
/>
<button
type="button"
@click="store()"
>Save</button>
</div>
Run Code Online (Sandbox Code Playgroud)
脚本
export default {
data() {
return {
saveForm: {
product_name: "",
product_image: null
}
};
},
methods: {
store() {
var config = {
headers: {
"Content-type":
"multipart/form-data; charset=utf-8; boundary=" +
Math.random()
.toString()
.substr(2),
processData: false,
Accept: "application/json"
}
};
var fileData = new FormData();
fileData.append("product_image", this.saveForm.product_image);
fileData.append("product_name", this.saveForm.product_name);
axios
.post("my route", fileData, config)
.then(response => {
if (response.data.Status) {
}
})
.catch(error => {
console.log(error);
});
},
uploadImage(e) {
this.saveForm.product_image = e.target.files[0];
}
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
257 次 |
| 最近记录: |