How to upload an image in React with formidable and expressjs










1















I triying to upload an image with formidable in React with express for save the image path into database .I readed these tutorials: sequelize crud with expressjs
and Simple File Upload with Express.js and Formidable in Node.js
the trouble that I have is that I can't mixed for upload images from React and save the URL into database.



I have this in my React Code for set the State:



picture(e)
this.setState(
picture:e.target.files[0]
);
console.log(e.target.files[0]);



Into my render() method I just handle it :



<input type="file" onChange=this.picture className="form-control-file" placeholder="Picture" name="picture"/>


Into my controller I have this code:



exports.create = (req, res) => 
// Save to MySQL database
var form = new formidable.IncomingForm();
var fileName;
form.parse(req);
form.on('fileBegin', function (name, file)
fileName='/home/public_html/react-panel/img/' + file.name;
file.path = '/home/public_html/react-panel/img/' + file.name;
console.log('fileBegin ' + fileName);
);
form.on('file', function (name, file)
console.log('Uploaded ' + file.name);
);
StrongDish.create(
id: req.body.id,
name: req.body.name,
description: req.body.description,
picture: fileName,
category: req.body.category,
price: req.body.price
).then(strongDish =>
// Send created customer to client
res.send(strongDish);
);
;


The other data keep save in the database except the ima file



For make the post I just call an action with redux sending the data of the state



export const addStrongDish=dish=>async dispatch=>
const response = await axios.post('http://www.my-domain.co.cr/api/dish/add/',dish);
dispatch(
type:ADD_DISH,
payload:response.data
)










share|improve this question




























    1















    I triying to upload an image with formidable in React with express for save the image path into database .I readed these tutorials: sequelize crud with expressjs
    and Simple File Upload with Express.js and Formidable in Node.js
    the trouble that I have is that I can't mixed for upload images from React and save the URL into database.



    I have this in my React Code for set the State:



    picture(e)
    this.setState(
    picture:e.target.files[0]
    );
    console.log(e.target.files[0]);



    Into my render() method I just handle it :



    <input type="file" onChange=this.picture className="form-control-file" placeholder="Picture" name="picture"/>


    Into my controller I have this code:



    exports.create = (req, res) => 
    // Save to MySQL database
    var form = new formidable.IncomingForm();
    var fileName;
    form.parse(req);
    form.on('fileBegin', function (name, file)
    fileName='/home/public_html/react-panel/img/' + file.name;
    file.path = '/home/public_html/react-panel/img/' + file.name;
    console.log('fileBegin ' + fileName);
    );
    form.on('file', function (name, file)
    console.log('Uploaded ' + file.name);
    );
    StrongDish.create(
    id: req.body.id,
    name: req.body.name,
    description: req.body.description,
    picture: fileName,
    category: req.body.category,
    price: req.body.price
    ).then(strongDish =>
    // Send created customer to client
    res.send(strongDish);
    );
    ;


    The other data keep save in the database except the ima file



    For make the post I just call an action with redux sending the data of the state



    export const addStrongDish=dish=>async dispatch=>
    const response = await axios.post('http://www.my-domain.co.cr/api/dish/add/',dish);
    dispatch(
    type:ADD_DISH,
    payload:response.data
    )










    share|improve this question


























      1












      1








      1








      I triying to upload an image with formidable in React with express for save the image path into database .I readed these tutorials: sequelize crud with expressjs
      and Simple File Upload with Express.js and Formidable in Node.js
      the trouble that I have is that I can't mixed for upload images from React and save the URL into database.



      I have this in my React Code for set the State:



      picture(e)
      this.setState(
      picture:e.target.files[0]
      );
      console.log(e.target.files[0]);



      Into my render() method I just handle it :



      <input type="file" onChange=this.picture className="form-control-file" placeholder="Picture" name="picture"/>


      Into my controller I have this code:



      exports.create = (req, res) => 
      // Save to MySQL database
      var form = new formidable.IncomingForm();
      var fileName;
      form.parse(req);
      form.on('fileBegin', function (name, file)
      fileName='/home/public_html/react-panel/img/' + file.name;
      file.path = '/home/public_html/react-panel/img/' + file.name;
      console.log('fileBegin ' + fileName);
      );
      form.on('file', function (name, file)
      console.log('Uploaded ' + file.name);
      );
      StrongDish.create(
      id: req.body.id,
      name: req.body.name,
      description: req.body.description,
      picture: fileName,
      category: req.body.category,
      price: req.body.price
      ).then(strongDish =>
      // Send created customer to client
      res.send(strongDish);
      );
      ;


      The other data keep save in the database except the ima file



      For make the post I just call an action with redux sending the data of the state



      export const addStrongDish=dish=>async dispatch=>
      const response = await axios.post('http://www.my-domain.co.cr/api/dish/add/',dish);
      dispatch(
      type:ADD_DISH,
      payload:response.data
      )










      share|improve this question
















      I triying to upload an image with formidable in React with express for save the image path into database .I readed these tutorials: sequelize crud with expressjs
      and Simple File Upload with Express.js and Formidable in Node.js
      the trouble that I have is that I can't mixed for upload images from React and save the URL into database.



      I have this in my React Code for set the State:



      picture(e)
      this.setState(
      picture:e.target.files[0]
      );
      console.log(e.target.files[0]);



      Into my render() method I just handle it :



      <input type="file" onChange=this.picture className="form-control-file" placeholder="Picture" name="picture"/>


      Into my controller I have this code:



      exports.create = (req, res) => 
      // Save to MySQL database
      var form = new formidable.IncomingForm();
      var fileName;
      form.parse(req);
      form.on('fileBegin', function (name, file)
      fileName='/home/public_html/react-panel/img/' + file.name;
      file.path = '/home/public_html/react-panel/img/' + file.name;
      console.log('fileBegin ' + fileName);
      );
      form.on('file', function (name, file)
      console.log('Uploaded ' + file.name);
      );
      StrongDish.create(
      id: req.body.id,
      name: req.body.name,
      description: req.body.description,
      picture: fileName,
      category: req.body.category,
      price: req.body.price
      ).then(strongDish =>
      // Send created customer to client
      res.send(strongDish);
      );
      ;


      The other data keep save in the database except the ima file



      For make the post I just call an action with redux sending the data of the state



      export const addStrongDish=dish=>async dispatch=>
      const response = await axios.post('http://www.my-domain.co.cr/api/dish/add/',dish);
      dispatch(
      type:ADD_DISH,
      payload:response.data
      )







      node.js reactjs express redux sequelize.js






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 0:02







      Leo1234562014

















      asked Nov 14 '18 at 23:47









      Leo1234562014Leo1234562014

      73110




      73110






















          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310464%2fhow-to-upload-an-image-in-react-with-formidable-and-expressjs%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310464%2fhow-to-upload-an-image-in-react-with-formidable-and-expressjs%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3