猿问

Javascript 文件到字符串到文件到 URL

我会将图像转换为字符串并将其从 ajax 发送到服务器,然后当我想从服务器获取它时,我会将其转换为文件而不是将其转换为图像 url,因此我进行了以下测试,但这是行不通的。


所以我需要你的帮助


<html>

  <head></head>

  <body>

    <input type="file"/>Please input a "png" image for test<br>

    <img src=""/>

    <hr>

    <button state="working">Working</button>

    <button state="notworking">Not Working</button>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <script>

      $.FileToString = async (File, callback) => {

          const reader = new FileReader()

          reader.onload = async () => {

              callback(await reader.result)

          }

          reader.readAsText(File)

      }

      

      $.StringToFile = (String, type)=>{

          return new Blob([String],{type:type})

      }

      

      $.FileToUrl = async (File,callback)=>{

          const reader = new FileReader()

          reader.onload = async ()=>{

              callback(await reader.result)

          }

          reader.readAsDataURL(File)

      }

    </script>

    <script>

      $("button[state='notworking']").on("click",()=>{

        $.FileToString($("input[type='file']")[0].files[0],(string)=>{

            $.FileToUrl($.StringToFile(String,"image/png"),(a)=>($("img")[0].src = a))

        })

      })

      

      $("button[state='working']").on("click",()=>{

        $.FileToUrl($("input[type='file']")[0].files[0],a=>($("img")[0].src = a))

      })

    </script>

  </body>

</html>


慕标5832272
浏览 274回答 1
1回答

天涯尽头无女友

您应该使用数组缓冲区作为中介<html>&nbsp; <head></head>&nbsp; <body>&nbsp; &nbsp; <input type="file"/>Please input a "png" image for test<br>&nbsp; &nbsp; <img src=""/>&nbsp; &nbsp; <hr>&nbsp; &nbsp; <button state="working">Working</button>&nbsp; &nbsp; <button state="notworking">now works</button>&nbsp; &nbsp; <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; $.FileToArrayBuffer = (File, callback) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const reader = new FileReader();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reader.onload = async () => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; callback(await reader.result);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reader.readAsArrayBuffer(File)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $.ArrayBufferToString = (ArrayBuffer, callback)=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; btoa(new Uint8Array(blob).reduce(async (data,byte)=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; callback(await (data + String.fromCharCode(byte)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $.StringToArrayBuffer = (String)=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var enc = TextEncoder();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return enc.encode(String);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $.ArrayBufferToFile = (ArrayBuffer)=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new Blob([ArrayBuffer])&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $.FileToUrl = (File,callback)=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const reader = new FileReader();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reader.onload = async ()=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; callback(await reader.result);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reader.readAsDataURL(File);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </script>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; $("button[state='notworking']").on("click",()=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.FileToArrayBuffer($("input[type='file']")[0].files[0],(arraybuffer)=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.FileToUrl($.ArrayBufferToFile(arraybuffer,"image/png"),(a)=>($("img")[0].src = a))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; $("button[state='working']").on("click",()=>{&nbsp; &nbsp; &nbsp; &nbsp; $.FileToUrl($("input[type='file']")[0].files[0],a=>($("img")[0].src = a))&nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; </script>&nbsp; </body></html>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答