JS BASE64转换

使用JS 最简单的代码进行text 到 base64 的转换,可以使用如下链接

http://aqwu.net/tools/base64.html

源代码如下:

<!DOCTYPE html>
<html lang="cn">
<head>
<title>base64 - 杰力皓博</title>
<meta charset="utf-8" />
<meta name="referrer" content="same-origin" />
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="keywords" content="base64,杰力皓博">
<style>
.center {
  margin: auto;
  width: 300px;
  border: 3px solid #73AD21;
  padding: 10px;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
  border-radius: 15px;
  background: #f0f0f0;
}
textarea {
  width: 290px;
}
</style>
</head>
<body>
<div class="center">
<center>
<h1>Base64</h1>
<hr/>
<table>
  <tr>
    <td>text:</td>
  </tr>
  <tr>
    <td>
      <textarea id="text" type="textarea" rows="10" placeholder="text"/></textarea>
    </td>
  </tr>
  <tr>
    <td>base64:</td>
  </tr>
  <tr>
    <td>
      <textarea id="base64" type="textarea" rows="10" placeholder="base64"></textarea>
    </td>
  </tr>
</table>
<hr/>
<table>
  <tr>
    <td><input id="encod" type="button" value="Encode" onclick="encodedData()"/></td>
    <td><input id="decoded" type="button" value="Decode" onclick="decodedData()"/></td>
  </tr>
</table>
</center>
</div>
<script type="text/javascript">
function encodedData()
{
  // 编码
  base64.value = window.btoa(unescape(encodeURIComponent(text.value)));
}
function decodedData()
{
  // 解码
  text.value = decodeURIComponent(escape(window.atob(base64.value)));
}

window.onload=function()
{
  text.value = "";
  base64.value = "";
}
</script>
</body>
</html>

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注