• <bdo id='t6lFB'></bdo><ul id='t6lFB'></ul>

    <small id='t6lFB'></small><noframes id='t6lFB'>

  1. <tfoot id='t6lFB'></tfoot>

  2. <i id='t6lFB'><tr id='t6lFB'><dt id='t6lFB'><q id='t6lFB'><span id='t6lFB'><b id='t6lFB'><form id='t6lFB'><ins id='t6lFB'></ins><ul id='t6lFB'></ul><sub id='t6lFB'></sub></form><legend id='t6lFB'></legend><bdo id='t6lFB'><pre id='t6lFB'><center id='t6lFB'></center></pre></bdo></b><th id='t6lFB'></th></span></q></dt></tr></i><div id='t6lFB'><tfoot id='t6lFB'></tfoot><dl id='t6lFB'><fieldset id='t6lFB'></fieldset></dl></div>
    <legend id='t6lFB'><style id='t6lFB'><dir id='t6lFB'><q id='t6lFB'></q></dir></style></legend>
    1. 如何将 Base64 字符串转换为 javascript 文件对象,如文件输入表单?

      时间:2023-10-01
    2. <small id='xHuGu'></small><noframes id='xHuGu'>

          <tfoot id='xHuGu'></tfoot>
            • <bdo id='xHuGu'></bdo><ul id='xHuGu'></ul>
              <i id='xHuGu'><tr id='xHuGu'><dt id='xHuGu'><q id='xHuGu'><span id='xHuGu'><b id='xHuGu'><form id='xHuGu'><ins id='xHuGu'></ins><ul id='xHuGu'></ul><sub id='xHuGu'></sub></form><legend id='xHuGu'></legend><bdo id='xHuGu'><pre id='xHuGu'><center id='xHuGu'></center></pre></bdo></b><th id='xHuGu'></th></span></q></dt></tr></i><div id='xHuGu'><tfoot id='xHuGu'></tfoot><dl id='xHuGu'><fieldset id='xHuGu'></fieldset></dl></div>
              <legend id='xHuGu'><style id='xHuGu'><dir id='xHuGu'><q id='xHuGu'></q></dir></style></legend>

                  <tbody id='xHuGu'></tbody>
                本文介绍了如何将 Base64 字符串转换为 javascript 文件对象,如文件输入表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想将从文件中提取的 Base64String(例如:AAAAA....~")转换为 javascript 文件对象.

                I want to convert Base64String extracted from file(ex: "AAAAA....~") to a javascript file object.

                我的意思的javascript文件对象是这样的代码:

                The javascript file object what I mean is like this code:

                HTML:

                <input type="file" id="selectFile" > 
                

                JS:

                $('#selectFile').on('change', function(e) {
                  var file = e.target.files[0];
                
                  console.log(file)
                }
                

                'file' 变量是一个 javascript 文件对象.所以我想像这样将base64字符串转换为javascript文件对象.

                'file' variable is a javascript file object. So I want to convert a base64 string to the javascript file object like that.

                我只想通过解码base64字符串(由其他应用程序从文件中编码)来获取文件对象,而不需要html文件输入表单.

                I just want to get file object by decoding base64 string (encoded by other app from a file) without html file input form.

                谢谢.

                推荐答案

                方式一:只适用于dataURL,不适用于其他类型的url.

                Way 1: only works for dataURL, not for other types of url.

                 function dataURLtoFile(dataurl, filename) {
                 
                        var arr = dataurl.split(','),
                            mime = arr[0].match(/:(.*?);/)[1],
                            bstr = atob(arr[1]), 
                            n = bstr.length, 
                            u8arr = new Uint8Array(n);
                            
                        while(n--){
                            u8arr[n] = bstr.charCodeAt(n);
                        }
                        
                        return new File([u8arr], filename, {type:mime});
                    }
                    
                    //Usage example:
                    var file = dataURLtoFile('data:text/plain;base64,aGVsbG8gd29ybGQ=','hello.txt');
                    console.log(file);

                方式 2: 适用于任何类型的 url,(http url、dataURL、blobURL 等...)

                Way 2: works for any type of url, (http url, dataURL, blobURL, etc...)

                 //return a promise that resolves with a File instance
                    function urltoFile(url, filename, mimeType){
                        return (fetch(url)
                            .then(function(res){return res.arrayBuffer();})
                            .then(function(buf){return new File([buf], filename,{type:mimeType});})
                        );
                    }
                    
                    //Usage example:
                    urltoFile('data:text/plain;base64,aGVsbG8gd29ybGQ=', 'hello.txt','text/plain')
                    .then(function(file){ console.log(file);});

                这篇关于如何将 Base64 字符串转换为 javascript 文件对象,如文件输入表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:图片转Base64 下一篇:使用javascript将二进制数据转换为base64

                相关文章

                    <legend id='ZIXus'><style id='ZIXus'><dir id='ZIXus'><q id='ZIXus'></q></dir></style></legend>
                      <bdo id='ZIXus'></bdo><ul id='ZIXus'></ul>

                    <tfoot id='ZIXus'></tfoot>
                  1. <i id='ZIXus'><tr id='ZIXus'><dt id='ZIXus'><q id='ZIXus'><span id='ZIXus'><b id='ZIXus'><form id='ZIXus'><ins id='ZIXus'></ins><ul id='ZIXus'></ul><sub id='ZIXus'></sub></form><legend id='ZIXus'></legend><bdo id='ZIXus'><pre id='ZIXus'><center id='ZIXus'></center></pre></bdo></b><th id='ZIXus'></th></span></q></dt></tr></i><div id='ZIXus'><tfoot id='ZIXus'></tfoot><dl id='ZIXus'><fieldset id='ZIXus'></fieldset></dl></div>

                    <small id='ZIXus'></small><noframes id='ZIXus'>