首页 > 解决方案 > 将图像转换为 blob 以存储在 phpmyadmin 中

问题描述

我有一个反应应用程序,它有一个基本的注册页面,其中一部分是允许用户上传个人资料照片。我在这里阅读了很多关于在服务器端发送图像的答案,但没有一个对我有用。为了克服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' }

任何有关这方面的帮助将不胜感激!

标签: javascriptnode.jsreactjsimageaxios

解决方案


推荐阅读