简体   繁体   中英

To upload a file what are the pros and cons of sending base64 in post body vs multipart/form-data

I am creating a REST api to upload a file (mostly 2-5 min video files), I have an option to do it like a post method with a base64 property in body of JSON request or handling multipart/form-data

Wondering what are the pros and cons of each approach

Let's start with pros because the list is shorter:

  • literally none

Cons:

  • You engage client's processor to transform the data from a binary file to a string
  • You close yourself a way to do partial upload (where you can upload a file in chunks and restore previous session when connection has been lost instead of starting over)
  • Base64 files are bigger than the original file - you are wasting user transfer (the user can use mobile device, some countries still have internet limits in a cable)
  • You have to reverse the process on the server which costs you more money as usually you pay for the CPU utilization

base64

converts binary data to ASCII representation. Overhead can be incurred by padding during this process, which typically increases in size by about 33-36%. wiki/base64

multipart/form-data

is a standard for sending binary data over HTTP requests. Encoding can be done for each part you send Unless special circumstances, I recommend using multipart upload.

In your current situation, it would be a good idea to use multipart/form-data.


If so, let's add as much as I can think of when to use base64.

  1. Data that must be visible and managed by humans must be expressed in ASCII as binary data.

  2. When sending and receiving binary data, if the decoding/encoding rules are different, you can use base64 to match. In other words, if the character sets used are different, base64 encoding is performed to match them. For example, if you need to put binary data in a text document like XML, you need to characterize it and go through an encoding method like base64.

In my personal opinion, base64 and multipart/form-data do not compare pros and cons, but rather have different purposes and characteristics.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM