Javascript ReactJs:无法通过客户端发布多种格式的数据
我正在创建一个带有图片上传的博客。但在这里,我面临几个问题,张贴图像到后端。然而,当我试图通过Javascript ReactJs:无法通过客户端发布多种格式的数据,javascript,node.js,reactjs,redux,file-upload,Javascript,Node.js,Reactjs,Redux,File Upload,我正在创建一个带有图片上传的博客。但在这里,我面临几个问题,张贴图像到后端。然而,当我试图通过POSTMAN表单数据发布此信息时,我的API可以通过multer将数据提交到cloudinary 当前错误:错误:多部分:未找到边界 我也尝试并查看了许多和github示例,但我无法解决当前代码的问题。请在此提供任何建议或帮助 //我的客户端代码 class BlogModal extends Component { state = { blog_short_desc: '',
POSTMAN
表单数据发布此信息时,我的API可以通过multer将数据提交到cloudinary
当前错误:错误:多部分:未找到边界
我也尝试并查看了许多和github
示例,但我无法解决当前代码的问题。请在此提供任何建议或帮助
//我的客户端代码
class BlogModal extends Component {
state = {
blog_short_desc: '',
blog_image_url: '',
blog_by_author: '',
FileName: null
}
onChange = (e) => {
this.setState({ [e.target.name] : e.target.value });
}
onFileChange = (e) => {
this.setState({ [e.target.name] : e.target.files[0] });
}
onSubmit = (e) => {
e.preventDefault();
const FileName = this.state.blog_image_url ;
const formData = new FormData();
formData.append("File", FileName);
const newBlog = {
blog_short_desc: this.state.blog_short_desc,
blog_image_url: formData,
blog_by_author: this.props.auth["user"].name
}
console.log(newBlog);
this.props.addBlog(newBlog);
alert("Blog added successfully!");
}
render(){
return(
<div>
<Form onSubmit ={this.onSubmit } enctype="multipart/form-data">
<FormGroup>
<Label for="blogHeading">Blog Heading</Label>
<Input type="text" name="blog_short_desc" id="blogHeading" placeholder="Add one liner"
onChange={this.onChange} required/>
<Label for="blogImageURl">Image Url</Label>
<Input type="file" name="blog_image_url" id="blogImageURl" placeholder="Add image url "
onChange={this.onFileChange} />
<Button color="dark" style={{marginTop: '2rem'}} block>Add blog</Button>
</FormGroup>
</Form>
</div>
)
}
}
const mapStateToProps = state => ({
resume: state.resume,
auth: state.auth
})
export default connect(mapStateToProps, { addBlog })(BlogModal);
//令牌配置
export const tokenFormConfig = getState => {
//Get token from local storage
const token = getState().auth.token;
// headers
const config = {
headers: {
Accept: 'application/json',
'Content-Type': 'multipart/form-data',
}
}
//if token then add to headers
if(token) {
config.headers['x-auth-token'] = token;
}
return config;
};
//博客路由器
router.post('/', upload.single('blog_image_url'), async (req, res, next) => {
try {
const result = await cloudinary.uploader.upload(req.file.path);
const newBlog = new Blog({
blog_short_desc: req.body.blog_short_desc,
blog_image_url: result.secure_url,
blog_by_author: req.body.blog_by_author
});
await newBlog.save().then(blogs => res.json(blogs));
next();
} catch (error) {
next(error);
}
});
//博客模式
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const BlogSchema = new Schema({
blog_short_desc: {
type: String,
required: true
},
blog_image_url: {
type: String,
required: true
},
blog_by_author:{
type: String,
required: true
},
date: {
type: Date,
default: Date.now
}
});
module.exports = Blog = mongoose.model('blog', BlogSchema);
//电流误差
`Error: Multipart: Boundary not found`
//当前用户界面
//通过邮递员表单数据提交时
FormData.append()
包含所有要发送的数据,并且应该将文件添加到其中,而不仅仅是名称
希望这有助于:
//博客模态
onSubmit=(e)=>{
e、 预防默认值();
const newBlog=new FormData();
追加(“blog\u short\u desc”,this.state.blog\u short\u desc);
append(“blog\u image\u url”,this.state.blog\u image\u url);
newBlog.append(“博客作者”,this.state.blog作者);
console.log(newBlog);
this.props.addBlog(newBlog);
警告(“博客添加成功!”);
}
//添加操作
export const addBlog=blog=>(dispatch,getState)=>{
axios({
url:“/api/blogs”,
方法:“post”,
数据:bodyFormData,
标题:{
“内容类型”:“多部分/表单数据”,
}
})
。然后(res=>
派遣({
类型:types.ADD_BLOG,
有效载荷:res.data
}))
.catch(err=>dispatch(returnErrors(err.response.data,err.response.status));
调试器
};
嗨,埃姆雷,谢谢你抽出时间。但我在这里也犯了错误。我已经更新了我的代码。编辑了答案以包含您的代码,希望能帮助您查看此答案:谢谢。你只是和蔼可亲。我只是在头撞了20个小时后才开始寻找这个解决方案。你终于解决了我的问题。非常感谢。
`Error: Multipart: Boundary not found`