javascript/python/flask - formData not sending uploaded file to server [duplicate]












0
















This question already has an answer here:




  • How to get data received in Flask request

    15 answers




I'm trying to send a user uploaded file to my server through an axios POST request with formData and it is not working.



What doesn't work:



const formData = new FormData();
formData.append('batch', batch);
formData.append('file', files[i]);

console.log(formData.get('batch')); // outputs 894489 correctly
console.log(formData.get('file')); // outputs fileObject correctly

axios.post('/api/upload', formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
});


response from python



print(request.form)
# ImmutableMultiDict([('batch', '894489')])
# Why is the file missing?


What does work:



const formData = new FormData();
formData.append('batch', batch);
formData.append('file', 'test');

console.log(formData.get('batch')); // outputs 894489 correctly
console.log(formData.get('file')); // outputs 'test' correctly

axios.post('/api/upload', formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
});


response from python



print(request.form)
# ImmutableMultiDict([('batch', '894489'), ('file', 'test')])
# Why is the file appearing?


My files[i] is a valid file and is being recognized in JS. This is confirmed with the console.log(formData.get('file')); which always outputs the correct file.



Somewhere along the way the file is lost and unavailable to python yet strangely if I append a string instead, it works. Any ideas?










share|improve this question













marked as duplicate by davidism flask
Users with the  flask badge can single-handedly close flask questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 1:53


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.























    0
















    This question already has an answer here:




    • How to get data received in Flask request

      15 answers




    I'm trying to send a user uploaded file to my server through an axios POST request with formData and it is not working.



    What doesn't work:



    const formData = new FormData();
    formData.append('batch', batch);
    formData.append('file', files[i]);

    console.log(formData.get('batch')); // outputs 894489 correctly
    console.log(formData.get('file')); // outputs fileObject correctly

    axios.post('/api/upload', formData, {
    headers: {
    'Content-Type': 'multipart/form-data'
    }
    });


    response from python



    print(request.form)
    # ImmutableMultiDict([('batch', '894489')])
    # Why is the file missing?


    What does work:



    const formData = new FormData();
    formData.append('batch', batch);
    formData.append('file', 'test');

    console.log(formData.get('batch')); // outputs 894489 correctly
    console.log(formData.get('file')); // outputs 'test' correctly

    axios.post('/api/upload', formData, {
    headers: {
    'Content-Type': 'multipart/form-data'
    }
    });


    response from python



    print(request.form)
    # ImmutableMultiDict([('batch', '894489'), ('file', 'test')])
    # Why is the file appearing?


    My files[i] is a valid file and is being recognized in JS. This is confirmed with the console.log(formData.get('file')); which always outputs the correct file.



    Somewhere along the way the file is lost and unavailable to python yet strangely if I append a string instead, it works. Any ideas?










    share|improve this question













    marked as duplicate by davidism flask
    Users with the  flask badge can single-handedly close flask questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 26 '18 at 1:53


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      0












      0








      0









      This question already has an answer here:




      • How to get data received in Flask request

        15 answers




      I'm trying to send a user uploaded file to my server through an axios POST request with formData and it is not working.



      What doesn't work:



      const formData = new FormData();
      formData.append('batch', batch);
      formData.append('file', files[i]);

      console.log(formData.get('batch')); // outputs 894489 correctly
      console.log(formData.get('file')); // outputs fileObject correctly

      axios.post('/api/upload', formData, {
      headers: {
      'Content-Type': 'multipart/form-data'
      }
      });


      response from python



      print(request.form)
      # ImmutableMultiDict([('batch', '894489')])
      # Why is the file missing?


      What does work:



      const formData = new FormData();
      formData.append('batch', batch);
      formData.append('file', 'test');

      console.log(formData.get('batch')); // outputs 894489 correctly
      console.log(formData.get('file')); // outputs 'test' correctly

      axios.post('/api/upload', formData, {
      headers: {
      'Content-Type': 'multipart/form-data'
      }
      });


      response from python



      print(request.form)
      # ImmutableMultiDict([('batch', '894489'), ('file', 'test')])
      # Why is the file appearing?


      My files[i] is a valid file and is being recognized in JS. This is confirmed with the console.log(formData.get('file')); which always outputs the correct file.



      Somewhere along the way the file is lost and unavailable to python yet strangely if I append a string instead, it works. Any ideas?










      share|improve this question















      This question already has an answer here:




      • How to get data received in Flask request

        15 answers




      I'm trying to send a user uploaded file to my server through an axios POST request with formData and it is not working.



      What doesn't work:



      const formData = new FormData();
      formData.append('batch', batch);
      formData.append('file', files[i]);

      console.log(formData.get('batch')); // outputs 894489 correctly
      console.log(formData.get('file')); // outputs fileObject correctly

      axios.post('/api/upload', formData, {
      headers: {
      'Content-Type': 'multipart/form-data'
      }
      });


      response from python



      print(request.form)
      # ImmutableMultiDict([('batch', '894489')])
      # Why is the file missing?


      What does work:



      const formData = new FormData();
      formData.append('batch', batch);
      formData.append('file', 'test');

      console.log(formData.get('batch')); // outputs 894489 correctly
      console.log(formData.get('file')); // outputs 'test' correctly

      axios.post('/api/upload', formData, {
      headers: {
      'Content-Type': 'multipart/form-data'
      }
      });


      response from python



      print(request.form)
      # ImmutableMultiDict([('batch', '894489'), ('file', 'test')])
      # Why is the file appearing?


      My files[i] is a valid file and is being recognized in JS. This is confirmed with the console.log(formData.get('file')); which always outputs the correct file.



      Somewhere along the way the file is lost and unavailable to python yet strangely if I append a string instead, it works. Any ideas?





      This question already has an answer here:




      • How to get data received in Flask request

        15 answers








      javascript python flask multipartform-data






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 23:16









      dariuscosdendariuscosden

      8018




      8018




      marked as duplicate by davidism flask
      Users with the  flask badge can single-handedly close flask questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 26 '18 at 1:53


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by davidism flask
      Users with the  flask badge can single-handedly close flask questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 26 '18 at 1:53


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes


















          0














          From inside Flask, the files should be accessible using this:



          request.files['file']





          share|improve this answer
























          • Thanks man. How did I not see it...

            – dariuscosden
            Nov 25 '18 at 23:35






          • 1





            Not your fault. I have to say sometimes I also get caught in some things when trying to make my front-end talk to flask. I always have to google it

            – Pedro Torres
            Nov 25 '18 at 23:40


















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          From inside Flask, the files should be accessible using this:



          request.files['file']





          share|improve this answer
























          • Thanks man. How did I not see it...

            – dariuscosden
            Nov 25 '18 at 23:35






          • 1





            Not your fault. I have to say sometimes I also get caught in some things when trying to make my front-end talk to flask. I always have to google it

            – Pedro Torres
            Nov 25 '18 at 23:40
















          0














          From inside Flask, the files should be accessible using this:



          request.files['file']





          share|improve this answer
























          • Thanks man. How did I not see it...

            – dariuscosden
            Nov 25 '18 at 23:35






          • 1





            Not your fault. I have to say sometimes I also get caught in some things when trying to make my front-end talk to flask. I always have to google it

            – Pedro Torres
            Nov 25 '18 at 23:40














          0












          0








          0







          From inside Flask, the files should be accessible using this:



          request.files['file']





          share|improve this answer













          From inside Flask, the files should be accessible using this:



          request.files['file']






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 23:32









          Pedro TorresPedro Torres

          683413




          683413













          • Thanks man. How did I not see it...

            – dariuscosden
            Nov 25 '18 at 23:35






          • 1





            Not your fault. I have to say sometimes I also get caught in some things when trying to make my front-end talk to flask. I always have to google it

            – Pedro Torres
            Nov 25 '18 at 23:40



















          • Thanks man. How did I not see it...

            – dariuscosden
            Nov 25 '18 at 23:35






          • 1





            Not your fault. I have to say sometimes I also get caught in some things when trying to make my front-end talk to flask. I always have to google it

            – Pedro Torres
            Nov 25 '18 at 23:40

















          Thanks man. How did I not see it...

          – dariuscosden
          Nov 25 '18 at 23:35





          Thanks man. How did I not see it...

          – dariuscosden
          Nov 25 '18 at 23:35




          1




          1





          Not your fault. I have to say sometimes I also get caught in some things when trying to make my front-end talk to flask. I always have to google it

          – Pedro Torres
          Nov 25 '18 at 23:40





          Not your fault. I have to say sometimes I also get caught in some things when trying to make my front-end talk to flask. I always have to google it

          – Pedro Torres
          Nov 25 '18 at 23:40



          Popular posts from this blog

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)