当前位置:网站首页>Why should Base64 encoding be used for image transmission

Why should Base64 encoding be used for image transmission

2022-07-04 14:14:00 Poets don't write poetry

One 、 Background knowledge

In general use HTTP agreement , The transmission is character text or binary data , this 2 These scenarios cover almost all uses HTTP The scenario of the agreement .HTTP Provisions of the agreement , Use Content-Type The request header specifies the type of transmission message , This determines how the requestor organizes the data ( Coding and format ) And how the receiver parses the data .Content-Type Commonly used values are as follows :
1、application/x-www-form-urlencoded
The data sent in this way is url Top splicing , With key1=val1&key2=val2 The way to code ,key and val It's all going on URL transcoding , This is a form submission method , A form means that there are multiple values .
2、multipart/form-data
From the name , The data transmitted is multiple , And it is the format of the form , This way is actually a powerful way , It can meet almost all data transmission requirements , It's just not very direct to use , So we only use this method to submit documents , Because files can only be transferred in the form of byte streams , Take transferring a file as an example , Need one key, You can call it myFile, Then we need to define value Type and content , The type is fille, The content is the file byte stream . The service provider needs to first according to myFIle Take out the file , Then resolve according to the type .
3、application/json
In this way, the data is expressed in json Format organization , Put it in body Send in .
4、text/xml
In this way, the data is expressed in xml Format organization , Put it in body Send in , This format has gradually been json replace , because ,json The format is more concise .

Two 、 The actual use

When we transmit pictures , It may be front-end and back-end interaction , It could be 2 Back end interaction , The treatment is usually different , Because the front end is responsible for interface interaction , Sending HTTP Requests are usually rich Content-Type, So when the front end sends pictures , Will use multipart/form-data This way, , This is also the default rule of the front and back ends .

But in 2 When interacting with multiple backend , The default data format is application/json, So we need to transform the image data , Usually , We got the byte stream of the picture byte[ ] after , Not in json Medium direct transmission byte[ ], Because I'm worried about the transmission process , There are services that implicitly encode it and then decode it , This causes the byte stream to change , So we do it at the transmission source base64 code , because base64 The use of ASCII Coding is supported by all coding methods , Even if a service encodes and decodes it during transmission , It can also be restored as is . So use base64 Coding is a security policy , however base64 It will increase the volume by about one third .

in addition ,base64 It is also a format directly supported by browsers . The following can be shown directly .

 

原网站

版权声明
本文为[Poets don't write poetry]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041152194859.html