0

When I upload a file on my app, I need it to be stored in a mysql db.

I tried to store it as a url, but I got error because it was too long; so I changed the type of the attribute in the db from string to blob, but now when I try to store the file, I get a 422 (unprocessable entity) error. I Think I should turn the url into an Object, but I don't know how. This is the code:

import React, { Component }     from 'react'
import PropTypes                from 'prop-types'
import { connect }              from 'react-redux'
import { bindActionCreators }   from 'redux'
import { withRouter, Link }     from 'react-router-dom'
import ProgressIndicator        from 'components/ProgressIndicator'
import * as assetActionCreators from 'core/actions/actions-asset'
import * as accountActionCreators from 'core/actions/actions-   account'
import { getString }            from 'core/libs/lib-asset-helpers'
import Controls                 from '../../components/Controls'
import { styles }               from './styles.scss'

class GenerateHashPanel extends Component { 
  constructor(props) {
    super(props) 
this.state = {
  assetUrl:'',
  nextBtnDisabled: true

   } 
  }

  componentDidMount() {
 const { actions, asset } = this.props

if (asset.assetHash === '') {
  getString(asset.stagedAsset, (assetUrl) => {
    setTimeout(() => {
      actions.asset.checkIfRegistered(assetUrl)
      actions.account.setAssetUrl(assetUrl)

      console.log("assetUrl", assetUrl)
    }, 2000) 
  })  
}


  }

  static getDerivedStateFromProps(nextProps) {
    const { asset } = nextProps

if (asset.assetHash !== '') {
  return { nextBtnDisabled: false }
} else if (asset.alreadyExists) {
  return { nextBtnDisabled: true }
}

return { nextBtnDisabled: true }
  }

  getControls = () => {
    const { nextBtnDisabled } = this.state
    return (
      <Controls
        prevDisabled={false}
        nextDisabled={nextBtnDisabled}
        handleNext={this.proceed}
      />
    )
  }

 proceed = () => {
   const { history } = this.props
    const {assetHash, owner, public_key, examDate, exam, assetUrl} = this.props.account
    assegnaUri(null, null, null, null, null, assetUrl)

    history.push('/register?panel=3')


 }

 render() {
   const { asset } = this.props
    const { alreadyExists, assetHash, error } = asset
let content

if (alreadyExists) {
  content = (
    <div className="notification">
      <h2>Spiacenti, questo documento è già stato registrato</h2>
      <span className="action"><Link to="/upload">Carica un nuovo documento</Link></span>
    </div>
  )
} else if (assetHash) {
  content = (
    <div>
      <h2>Codice univoco del documento</h2>
      <span>Premere "Avanti" per registrare il documento</span>
      <div id="unique-hash">{assetHash}</div>
    </div>
  )
} else if (error) {
  content = (
    <div className="notification">
      <h2>Spiacent, c&apos;è un errore!</h2>
      <span className="action"><Link to="/upload"> Hai effettuato l'accesso a Metamask <br /> e questo sta puntando sulla rete corretta??</Link></span>
    </div>
  )
} else if (asset.stagedAsset) {
  content = (
    <div>
      <h2>Sto generando un codice univoco per il documento...</h2>
      <div id="hash-progress-indicator">
        <ProgressIndicator type="linear" />
        <span className="blink">Attendere...</span>
      </div>
    </div>
  )
} else {
  content = (
    <div className="notification">
      <h2>Per favore, carica un documento da registrare</h2>
      <span className="action"><Link to="/home">Upload an asset</Link></span>
    </div>
  )
}

return (
  <div className={styles}>
    {content}
    {this.getControls()}
  </div>
)
  }
 }

function assegnaUri(hash_referto, owner, public_key, data_esame,    tipo_esame, uri) {

  fetch('http://localhost:8080/api/REFERTOs', {
    method: 'POST',
    headers: { 
      Accept: 'application/json',
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      "hash_referto":hash_referto,
      "owner":owner,
      "public_key":public_key,
      "data_esame": data_esame, 
      "tipo_esame":tipo_esame,
      "uri":uri
      }),
});
} 

GenerateHashPanel.propTypes = {
  actions: PropTypes.shape({}).isRequired,
  asset: PropTypes.shape({}).isRequired,
  account: PropTypes.shape({}).isRequired,
  history: PropTypes.shape({}).isRequired
}

function mapStateToProps(state) {
  return {
    asset: state.asset,
    account: state.account
  }
}

function mapDispatchToProps(dispatch) {
  return {
    actions: {
      asset: bindActionCreators(assetActionCreators, dispatch),
      account: bindActionCreators(accountActionCreators, dispatch)
    }
  }
}



export default withRouter(connect(mapStateToProps, mapDispatchToProps)(GenerateHashPanel))

Any advice?

mikirug88
  • 51
  • 7
  • Have you considered converting the contents of the file into base64 and storing the result in a text field? – ChrisFNZ Aug 18 '19 at 23:22
  • I think it already is base64, this is the url "assetUrl data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hy...and so on" – mikirug88 Aug 19 '19 at 08:09
  • So perhaps a longtext field type is best. When you need to display the retrieved image, refer to the following article: https://stackoverflow.com/questions/8499633/how-to-display-base64-images-in-html – ChrisFNZ Aug 19 '19 at 08:27
  • Thank you ChrisFNZ, with longtext field type i get now "POST http://localhost:8080/api/REFERTOs 500 (Internal Server Error)" – mikirug88 Aug 19 '19 at 09:14
  • I'm not a Node.js expert and I can't see the REFERTOs method in any case. Perhaps try firstly by manually setting some small dummy values, if that works try a small file and if that works, check to see if there's a max size setting somewhere. – ChrisFNZ Aug 19 '19 at 09:21
  • did it. i modified url and json limits in the server config. it works now. Thank you! – mikirug88 Aug 19 '19 at 13:15
  • I'm glad that my suggestions worked for you! – ChrisFNZ Aug 19 '19 at 21:12

0 Answers0