javascript 在reactjs中使用strapi api链接上传图像和内容

uinbv5nw  于 2023-01-29  发布在  Java
关注(0)|答案(1)|浏览(142)

我第一次尝试使用strapi和react,我不明白我怎么能链接上传(在strapi)图片到我的内容,我知道如何上传,我知道如何发布的东西,但我不知道如何链接这一点。我读了很多次strapi的文档,但我不能理解。
我的代码

function ProductCreateApi({ evtId }) {
  const [image, setImage] = useState(null)
  const [posts, setPosts] = useState([])
  const [updatesData, setUpdatesData] = useState({
    titleproductgroup: "",
  })

  function updateEdit(e) {

    const newupdate = { ...updatesData }
    newupdate[e.target.id] = e.target.value
    setUpdatesData(newupdate)
    console.log(newupdate)
  }

  const handleSubmit = async (e) => {
    console.log('handleSubmit')
    e.preventDefault()

    const formData = new FormData()
    formData.append('files', image) // the pic
    formData.append('ref', 'api::product-group.product-group') // link with my table
    formData.append('refId', evtId)
    formData.append('field', 'picproductgroup') // the row
    axios.post('http://localhost:1337/api/upload/', formData)

    e.preventDefault()
    const res = axios.post(`http://localhost:1337/api/product-groups/`, {
      "data": {
        titleproductgroup: updatesData.titleproductgroup
      }
    })

    if (res.ok) {
      console.log('res.ok')
      console.log('res', res)
      // imageUploaded()
    }
  }

  const handleFileChange = (e) => {
    console.log('handleFileChange')
    console.log(e.target.files[0]) //this will give us an array and we want the first wone so we add 0
    setImage(e.target.files[0])
  }
  return (
    <div>
      <h1> Upload Event Image</h1>

      <form onSubmit={handleSubmit}>
        <input onChange={(e) => updateEdit(e)} id="titleproductgroup" value={updatesData.titleproductgroup} type="text" placeholder={posts.titleproductgroup} />
        <div>
          <input type='file' onChange={handleFileChange} />
        </div>
        <input type='submit' value='Upload' className='btn' />
      </form>
    </div>
  )
}
export default ProductCreateApi

在注解中,我写下了我对属性的理解
这是我的"table"
谢谢你的帮助,我希望我能提高自己

5tmbdcev

5tmbdcev1#

我找到解决办法,我只是改变它

const handleSubmit = async (e) => {
    console.log('handleSubmit')
    e.preventDefault()

    const formData = new FormData()
    formData.append('files', image) // the pic
    formData.append('ref', 'api::product-group.product-group') // link with my table
    formData.append('refId', evtId)
    //formData.append('field', 'picproductgroup') // the row
    axios.post('http://localhost:1337/api/upload/', formData).then(res => {
      console.log(res.data[0].id);
      const res2 = axios.post(`http://localhost:1337/api/product-groups/`, {
        "data": {
          titleproductgroup: updatesData.titleproductgroup,
          picproductgroup: res.data[0].id,
        }
      })

      if (res2.ok) {
        console.log('res.ok')
        console.log('res', res2)
        // imageUploaded()
      }
    }).catch(error => {
      console.log(error.message);
    });

    //e.preventDefault()
  }

  const handleFileChange = (e) => {
    console.log('handleFileChange')
    console.log(e.target.files[0]) //this will give us an array and we want the first wone so we add 0
    setImage(e.target.files[0])
  }
  return (

相关问题