当前位置:网站首页>为什么图片传输要使用base64编码
为什么图片传输要使用base64编码
2022-07-04 12:51:00 【诗人不写诗】
一、背景知识
一般使用HTTP协议,传输的是字符文本或二进制数据,这2种场景覆盖了几乎所有使用HTTP协议的场景。HTTP协议规定,使用Content-Type请求头来约定传输消息的类型,这决定了请求方如何组织数据(编码和格式)以及接收方如何解析数据。Content-Type常用的取值有如下几种:
1、application/x-www-form-urlencoded
这种方式发送的数据在url上拼接,以key1=val1&key2=val2 的方式进行编码,key和val都进行了 URL转码,这是一种表单提交方式,表单意思是有多个值。
2、multipart/form-data
从名字上看,传输的数据是多份的,且是表单的格式,这种方式其实是一个功能强大的方式,几乎可以满足所有数据传输要求,只是使用起来不太直接,所以我们只使用这种方式来提交文件,因为文件只能以字节流的形式传输,以传输一个文件为例,需要一个key,可以命名为myFile,然后需要定义value 类型和内容,类型是fille,内容就是文件字节流。服务方需要先根据myFIle取出文件,然后根据类型解析。
3、application/json
这种方式将数据以json格式组织,放在body里发送。
4、text/xml
这种方式将数据以xml格式组织,放在body里发送,这种格式逐渐被json取代,因为,json格式更加简洁明了。
二、实际使用
我们在传输图片时,有可能是前后端交互,有可能是2个后端交互,处理方式通常不同,由于前端负责界面交互,在发送HTTP请求时通常具备丰富的Content-Type,所以前端在发送图片时,就会使用multipart/form-data这种方式,这也是前后端默认的规则。
但是在2个后端进行交互时,默认的数据格式是application/json,所以对图片这种数据就要进行转化,通常,我们在得到了图片的字节流byte[ ]后,不会在json中直接传输byte[ ],因为担心传输过程中,有服务对其进行隐式编码然后解码,这就导致字节流发生了变化,所以我们在传输源头就进行base64编码,因为base64使用的ASCII编码是所有编码方式都支持的,即使传输过程中有服务对其进行编解码,也能原样还原。所以使用base64编码是一种安全策略,但是base64会导致体积增大约三分之一。
另外,base64也是浏览器直接支持的一种格式。如下可以直接展示。
边栏推荐
- R语言ggplot2可视化:gganimate包创建动态折线图动画(gif)、使用transition_reveal函数在动画中沿给定维度逐步显示数据
- Install MySQL
- QT how to detect whether the mouse is on a control
- Unity shader learning (3) try to draw a circle
- [antd step pit] antd form cooperates with input Form The height occupied by item is incorrect
- Golang uses JSON unmarshal number to interface{} number to become float64 type (turn)
- xshell/bash/zsh 等终端鼠标滚轮乱码问题(转)
- Five "potential errors" in embedded programming
- 吃透Chisel语言.12.Chisel项目构建、运行和测试(四)——Chisel测试之ChiselTest
- SCM polling program framework based on linked list management
猜你喜欢

TestSuite and testrunner in unittest

Haobo medical sprint technology innovation board: annual revenue of 260million Yonggang and Shen Zhiqun are the actual controllers
![[antd step pit] antd form cooperates with input Form The height occupied by item is incorrect](/img/96/379d1692f9d3c05a7af2e938cbc5d7.png)
[antd step pit] antd form cooperates with input Form The height occupied by item is incorrect

【FAQ】華為帳號服務報錯 907135701的常見原因總結和解决方法

2022g3 boiler water treatment examination question simulation examination question bank and simulation examination

Understand chisel language thoroughly 06. Chisel Foundation (III) -- registers and counters

国内酒店交易DDD应用与实践——代码篇

C# wpf 实现截屏框实时截屏功能

Understand chisel language thoroughly 10. Chisel project construction, operation and testing (II) -- Verilog code generation in chisel & chisel development process

【Matlab】conv、filter、conv2、filter2和imfilter卷积函数总结
随机推荐
2022g3 boiler water treatment examination question simulation examination question bank and simulation examination
Understand chisel language thoroughly 03. Write to the developer of Verilog to chisel (you can also see it without Verilog Foundation)
吃透Chisel语言.07.Chisel基础(四)——Bundle和Vec
Whether the loyalty agreement has legal effect
SCM polling program framework based on linked list management
Summary of recent days (non-technical article)
好博医疗冲刺科创板:年营收2.6亿 万永钢和沈智群为实控人
File creation, writing, reading, deletion (transfer) in go language
常见 content-type对应表
C# wpf 实现截屏框实时截屏功能
Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
The mouse wheel of xshell/bash/zsh and other terminals is garbled (turn)
吃透Chisel语言.03.写给Verilog转Chisel的开发者(没有Verilog基础也可以看看)
吃透Chisel语言.08.Chisel基础(五)——Wire、Reg和IO,以及如何理解Chisel生成硬件
读取 Excel 表数据
【C 题集】of Ⅶ
Understand chisel language thoroughly 11. Chisel project construction, operation and test (III) -- scalatest of chisel test
TestSuite and testrunner in unittest
Programmer anxiety
Ruichengxin micro sprint technology innovation board: annual revenue of 367million, proposed to raise 1.3 billion, Datang Telecom is a shareholder