Win a copy of Kotlin Cookbook this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

How to parse coming request document with multipart/form-data into Node js ?

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Currently i am working on Node JS and get stuck.

Actually normal document is passed to my current node js API with 'content-type : application/json' and node js can upload it properly.

But now i have to accept document with 'content-type : multipart/form-data' into my node js api but it is not working properly.

Can you guys help me how to handle multipart document ?
 
Saloon Keeper
Posts: 11020
243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A quick search online reveals that there are a multitude of NPM packages for multipart parsing.

What have you tried? What are you stuck on?
 
SunilK Chauhan
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes there are multiple libs available to work with multipart form data. Let me give you one of them what I have tried.

Have given below code in UI side to upload doc as multipart :
 <form action="url" method="post" enctype="multipart/form-data">

And than applied below logic at back end means node js side to handle coming multi part document :
https://codeburst.io/how-to-handle-multipart-form-data-in-nodejs-file-uploading-in-nodejs-26c0cb88adcf

But seems like it's not getting upload any how.

From POST request coming from UI side is in json format including document in base64 encoding and i need to upload that base64 encoded file to database.

But using above logic its not getting upload as it's not able to parse coming json formatted data due to 'content-type:multipart /data-form' given in request header.

Do you know hoe to handle this type at node js?

This is my issue. Hope now you understand it propely.
 
Stephan van Hulst
Saloon Keeper
Posts: 11020
243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

SunilK Chauhan wrote:From POST request coming from UI side is in json format including document in base64 encoding and i need to upload that base64 encoded file to database.


No, it's not JSON. It's binary form data. That's the whole point of using multipart/form-data.

Use enctype="multipart/form-data" in your form and use a type="file" for your file input field. In the backend, use a library to parse the form. Depending on the library, you might get an array of bytes or it might encode the file for you to Base64 directly.

If you have trouble, show us your complete HTML form and show us the code you wrote to parse the request.
 
SunilK Chauhan
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have set enctype="multipart/form-data" in my form.

From post man, i am passing data as shown below:



In my Node JS api,


Note : base64data is my file already encoded coming from front-end.

Can you help me how to get read data from request here and how to upload it as multipart file to server ?
 
Stephan van Hulst
Saloon Keeper
Posts: 11020
243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the late reply, I missed your message.

You don't seem to get that multipart/form-data IS NOT Base64. It's raw bytes. Either your client sends multipart/form-data, or it sends Base64. You can't let the library decode Base64 if it expects multipart/form-data.

WHY are you encoding your binary data as Base64? Why are you not just letting HTTP do what HTTP does best and send your data as raw binary?

HOW are you sending Base64 if you first showed us that you're using an HTML form with enctype="multipart/form-data"?
 
Saloon Keeper
Posts: 21478
144
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:
WHY are you encoding your binary data as Base64? Why are you not just letting HTTP do what HTTP does best and send your data as raw binary?



Actually, raw binary is expected to be (automatically) encoded into Base64 by the client (as part of the form encoding) and automatically decoded by the server's request parser. So at the client side and in the server application code the data is indeed binary, but while in transit, it's base64. This is for historical reasons, as the early Internet was comprised of many different types of machines, some of which spoke ASCII, many of which spoke EBCDIC (IBM mainframes), some of which spoke more esoteric things, and a "byte" could be 6, 7, 8 or even more bits, but the one thing they all agreed on was that whatever their native code pages were, the characters A-Z and 0-9 had some sort of local representation, so unlike raw binary, character (Base64) encoding could be transported to and through all those different types of machines without losing or scrambling anything. Or getting all confused by byte ordering. This is, by the way, HTML, not HTTP. HTTP is the transport. HTML is the data encoding, and it's always text*. Because of the previously-mentioned constraints.

A "multipart" form is simply a form with multiple field values in it. But, as I said, the encoding and decoding is done automatically by the client and server programs, not the apps or client-side logic. Providing, of course, that you defined the form properly.

And, in short, that means that if you read your Node docs, you should find that incoming HTTP(S) requests include a request object and that the form field values are already accessible by field name in that request object. Uploaded files are a bit different, since they have to be pulled from whatever internal storage the webapp server is holding the actual file data in, but still, it's already done for you.

====
* "text" in its raw form. Any encryption and/or compression of the HTML request and response data is outside the scope of this issue.
 
SunilK Chauhan
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it's correct. I need to send types as file only instead of base64 encoded data. Now it's working fine.

Thanks for clarification.
 
What's brown and sticky? ... a stick. Or a tiny ad.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!