微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ReactJS:伪路径问题,但我需要正确的文件路径

如何解决ReactJS:伪路径问题,但我需要正确的文件路径

我必须在S3存储桶上上传图像,并且需要具有正确路径的文件对象。目前,当我上传图片时,我会在控制台中看到以下内容

 C:\fakepath\download (1).jpeg

并将其上传到s3存储桶中时,我得到的是“未定义”,如下所示:

https://test-profile-images.s3.amazonaws.com/undefined

基本上,我创建了一个中继器。下面是我的代码。 我该如何解决

import React,{useState} from "react"

const BrandDetails = (props) => {
  return (
    props.brandDetail !== '' ? 
    props.brandDetail.map((val,idx) => {
      let model = ` model-${idx}`
      return (
        <tr key={val.index}>
          <td>
            <input className="form-control"  defaultValue={val.model} name="model" data-id={idx} id={model} accept="image/x-png,image/jpeg,image/jpg,image/webp" type="file" required/>
          </td>  
        </tr >
      )
    })
    : null
  )
}
export default BrandDetails
import React,{Fragment,Component} from 'react'
import Breadcrumb from '../../common/breadcrumb'
import Brand from '../brands'
import BrandDetails from './brandRepeater'
import { uploadImage } from '../../common/imageUploader'

class CreateBrand extends Component {
    constructor(props) {
        super(props);

        this.brandobj = new Brand();
        this.state = {
            brandDetail: [{ index: Math.random(),model:"" }],}
    }

    onSubmit = (e) => {
        let params = ''

            this.state.brandDetail.map( detail => {
                console.log('image url : ',detail['model'])

                uploadImage(detail['model'])
                .then(res => {
                    console.log('uploaded url',res)
                    let asset = {
                        'src': res.location,'width': 400,'height': 400,'main': true
                    }
                 })
              })
        }

  addNewRow = (e) => {
        this.setState((prevstate) => ({
            brandDetail: [...prevstate.brandDetail,{ index: Math.random(),model:""}],}));
    }

    deteteRow = (index) => {
        this.setState({
            brandDetail: this.state.brandDetail.filter((s,sindex) => index !== sindex),});
    }

    clickOnDelete(record) {
        this.setState({
            brandDetail: this.state.brandDetail.filter(r => r !== record)
        });
    }

render() {
  return {
    <Fragment>
     <table className="table mt-3">
       <thead>
         <tr>
           <th>Model</th>
         </tr>
       </thead>  
      <tbody>
         <BrandDetails add={this.addNewRow} delete={this.clickOnDelete.bind(this)} brandDetail={brandDetail} flag={flag} /> 
      </tbody>      
   </table>
  </Fragment>
 }
}
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。