把文件转换成data:*/*;base64

经常会遇到转换文件为bas64格式,可以使用如下链接

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

源代码如下:

<!DOCTYPE html>
<html lang="cn">
<head>
<title>杰力皓博</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="杰力皓博">
<style>
.content{
  width: 400px;
  margin: auto;
}
</style>
</head>
<body>
<script>
window.onload=function()
{
  prv_staus.value = "";
  prvfile.onchange=function(e)
  {
    var files = this.files;
    console.log(files);
    var selectedfile = files[0];
    
    var reader = new FileReader();
    reader.readAsDataURL(selectedfile);

    reader.onload = function(event){
      prv_staus.value = event.target.result;
    }
  }
}

</script>
<div class="content">
  <h1>选择文件转成base64</h1>
  <br/>
  <input type="file" id="prvfile"/>
  <br/>
  <textarea id="prv_staus" name=""  rows="25" cols="50" placeholder="选择文件文件"></textarea>
</div>
</body>
</html>

发表评论

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