我有一个具有基本注册页面的react应用,其中一部分是允许用户上传个人资料照片。我在这里阅读了许多有关在服务器端发送图像的答案,但是没有一个对我有用。克服C:\\fakepath\\img-name
。我是否可以将图像转换为缓冲数组,以与其他输入字段一起发送?
这是我将数据发送到api的方式:
handleSubmit(){
let fname = this.state.fname;
let lname = this.state.lname;
let email = this.state.email;
let password = this.state.password;
let mentor = this.state.mentor;
let mentee = this.state.mentee;
let photo = this.state.photo
let interest1 = this.state.interest1;
let interest2 = this.state.interest2;
let interest3 = this.state.interest3;
let course = this.state.course;
const requestBody = {
fname: fname,
lname: lname,
email: email,
password: password,
photo: photo,
mentor: mentor,
mentee: mentee,
interest1: interest1,
interest2: interest2,
interest3: interest3,
course: course
}
const config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}}
if(this.checkValidation()) {
request.post("/register",qs.stringify(requestBody),config)
.then(result => this.setState({submitStatus: result.status}))
.catch(err => console.log(err))
}
}
当我将其提交到api服务器时,我得到了:
{ fname: 'Naruto',
lname: 'Uzumaki',
email: 'Naruto@konoha.com',
password: 'Hokage',
mentor: 'on',
mentee: '',
interest1: 'Anime',
interest2: 'Football',
interest3: 'Gaming',
course: 'CPSS' }
在此方面的任何帮助将不胜感激!