0

I'm new to node.js. I need to create and submit a form with a file to a url.

What is the way to do it?

I'm not able to find clear information about it.

I don't want to use form-data to do this simple thing moreover it requires a series of dependent modules.

I've used nodejs-websocket module to create a server which listens from browser clients.

user5858
  • 1,082
  • 4
  • 39
  • 79

1 Answers1

0

You can try using express/multer for handling file upload in the server side. Its much simple.

Use an AJAX request on the client side to upload the file when the button is clicked.

<form role="form" enctype="multipart/form-data" >
   .....
   <input type="file"/>
<form />


$('#upload_button_id').click(function (event) { 
  $('#form_id').on('submit', function(e) { 
    e.preventDefault(); 

    var formData = new FormData($('form')[0]); 

    $.ajax({
      type:'post,'
      url: '/url/fileupload/', //the URL to your node.js server that has data
      dataType: 'json',
      data: formData, 
      cache: false, 
      contentType: false, 
      processData: false, 
      success: function(response){
         //file was uploaded successfuly 
         alert(response);
      },
      error: function(){
         //Something went wrong
      } 
   });
  });
});