filereader http://www.e-learn.cn/tag/filereader zh-hans PC8 / CP437 character set with filereader in Chrome http://www.e-learn.cn/topic/4117691 <span>PC8 / CP437 character set with filereader in Chrome</span> <span><span lang="" about="/user/40" typeof="schema:Person" property="schema:name" datatype="">こ雲淡風輕ζ</span></span> <span>2021-02-19 07:48:35</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>anyone knows if it is possible to get the FileReader API in chrome to read a file with the CP437 character set? Is there a place where I can list the available encodings? Currently, my workaround is to read it with CP1251 <code>reader.readAsText(file, 'CP1251')</code> and manually replace special characters, which is not cool! Is there other browsers which support this character set? Or do you have any better idea at a workaround?</p> <p>Edit: The file is parsed only in the browser, there is no backend available.</p> <p>regards Oskar</p> <br /><h3>回答1:</h3><br /><p>I had the same problem. CP437 isn't implemented (in Chrome and Firefox at least). </p> <p>Work around to convert to UTF-8 that requires <code>ArrayBuffer</code> support in the browser:</p> <pre><code>var Cp437Helper = function () { var cp437map = [ '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', '\u0006', '\u0007', '\u0008', '\u0009', '\u000A', '\u000B', '\u000C', '\u000D', '\u000E', '\u000F', '\u0010', '\u0011', '\u0012', '\u0013', '\u0014', '\u0015', '\u0016', '\u0017', '\u0018', '\u0019', '\u001A', '\u001B', '\u001C', '\u001D', '\u001E', '\u001F', '\u0020', '\u0021', '\u0022', '\u0023', '\u0024', '\u0025', '\u0026', '\u0027', '\u0028', '\u0029', '\u002A', '\u002B', '\u002C', '\u002D', '\u002E', '\u002F', '\u0030', '\u0031', '\u0032', '\u0033', '\u0034', '\u0035', '\u0036', '\u0037', '\u0038', '\u0039', '\u003A', '\u003B', '\u003C', '\u003D', '\u003E', '\u003F', '\u0040', '\u0041', '\u0042', '\u0043', '\u0044', '\u0045', '\u0046', '\u0047', '\u0048', '\u0049', '\u004A', '\u004B', '\u004C', '\u004D', '\u004E', '\u004F', '\u0050', '\u0051', '\u0052', '\u0053', '\u0054', '\u0055', '\u0056', '\u0057', '\u0058', '\u0059', '\u005A', '\u005B', '\u005C', '\u005D', '\u005E', '\u005F', '\u0060', '\u0061', '\u0062', '\u0063', '\u0064', '\u0065', '\u0066', '\u0067', '\u0068', '\u0069', '\u006A', '\u006B', '\u006C', '\u006D', '\u006E', '\u006F', '\u0070', '\u0071', '\u0072', '\u0073', '\u0074', '\u0075', '\u0076', '\u0077', '\u0078', '\u0079', '\u007A', '\u007B', '\u007C', '\u007D', '\u007E', '\u007F', '\u00C7', '\u00FC', '\u00E9', '\u00E2', '\u00E4', '\u00E0', '\u00E5', '\u00E7', '\u00EA', '\u00EB', '\u00E8', '\u00EF', '\u00EE', '\u00EC', '\u00C4', '\u00C5', '\u00C9', '\u00E6', '\u00C6', '\u00F4', '\u00F6', '\u00F2', '\u00FB', '\u00F9', '\u00FF', '\u00D6', '\u00DC', '\u00A2', '\u00A3', '\u00A5', '\u20A7', '\u0192', '\u00E1', '\u00ED', '\u00F3', '\u00FA', '\u00F1', '\u00D1', '\u00AA', '\u00BA', '\u00BF', '\u2310', '\u00AC', '\u00BD', '\u00BC', '\u00A1', '\u00AB', '\u00BB', '\u2591', '\u2592', '\u2593', '\u2502', '\u2524', '\u2561', '\u2562', '\u2556', '\u2555', '\u2563', '\u2551', '\u2557', '\u255D', '\u255C', '\u255B', '\u2510', '\u2514', '\u2534', '\u252C', '\u251C', '\u2500', '\u253C', '\u255E', '\u255F', '\u255A', '\u2554', '\u2569', '\u2566', '\u2560', '\u2550', '\u256C', '\u2567', '\u2568', '\u2564', '\u2565', '\u2559', '\u2558', '\u2552', '\u2553', '\u256B', '\u256A', '\u2518', '\u250C', '\u2588', '\u2584', '\u258C', '\u2590', '\u2580', '\u03B1', '\u00DF', '\u0393', '\u03C0', '\u03A3', '\u03C3', '\u00B5', '\u03C4', '\u03A6', '\u0398', '\u03A9', '\u03B4', '\u221E', '\u03C6', '\u03B5', '\u2229', '\u2261', '\u00B1', '\u2265', '\u2264', '\u2320', '\u2321', '\u00F7', '\u2248', '\u00B0', '\u2219', '\u00B7', '\u221A', '\u207F', '\u00B2', '\u25A0', '\u00A0' ]; this.convertToUTF8 = function (buffer) { var out = ''; var view = new DataView(buffer); var i ; var n = view.byteLength; for (i = 0; i &lt; n; i ++) { var uint = view.getUint8(i); if (cp437map[ uint ] == undefined) { out += '/' + uint + '/'; } else { out += cp437map[ uint ]; } } return out; }; if ( Cp437Helper.prototype._instance == undefined ) { Cp437Helper.prototype._instance = this; } return Cp437Helper.prototype._instance; }; </code></pre> <p>Use the above:</p> <pre><code>var getTextFromCP437File = function ( file ) { var reader = new FileReader(); reader.onload = function(e) { var text = Cp437Helper().convertToUTF8(reader.result); // do something... }; //reader.readAsText(file, 'cp437'); reader.readAsArrayBuffer(file); }; </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/17815444/pc8-cp437-character-set-with-filereader-in-chrome</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/javascript" hreflang="zh-hans">javascript</a></div> <div class="field--item"><a href="/tag/html" hreflang="zh-hans">html</a></div> <div class="field--item"><a href="/tag/google-chrome" hreflang="zh-hans">google-chrome</a></div> <div class="field--item"><a href="/tag/character-encoding" hreflang="zh-hans">character-encoding</a></div> <div class="field--item"><a href="/tag/filereader" hreflang="zh-hans">filereader</a></div> </div> </div> Thu, 18 Feb 2021 23:48:35 +0000 こ雲淡風輕ζ 4117691 at http://www.e-learn.cn Javascript - Save typed array as blob and read back in as binary data http://www.e-learn.cn/topic/4112539 <span>Javascript - Save typed array as blob and read back in as binary data</span> <span><span lang="" about="/user/125" typeof="schema:Person" property="schema:name" datatype="">僤鯓⒐⒋嵵緔</span></span> <span>2021-02-18 10:54:06</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I have a typed array full of binary data that is being generated from an ArrayBuffer</p> <pre><code>var myArr = new Uint8Array(myBuffer); </code></pre> <p>I am presenting this to the user with </p> <pre><code>var blob = new Blob(myArr, {type: "octet/stream"}; var blobURL = URL.createObjectURL(blob); </code></pre> <p>and inserting a link that is</p> <pre><code>"&lt;a href=" + blobUrl + " download=" + filename "/a&gt;" </code></pre> <p>Later, I am letting the user select the file from disk, and using a file reader to do with</p> <pre><code>var reader = new FileReader(); reader.onload = function () { console.log(reader.result); }; reader.readAsArrayBuffer(sourceFile); </code></pre> <p>The problem is, it seems like no matter what I do, I get a "string" of the file's contents. In fact, when I save the file, I can open it, and it is plainly human readable. I.E. if my Uint8Array was <code>{"0" : "51", "1" : "52", "2" : "53" }</code></p> <p>I can open the downloaded blob in a text editor and I just see "515253" which I don't think is what should be happening.</p> <p>How can I make a link to a file download for my file that is formatted properly so I can read it back in an dget the right values?</p> <br /><h3>回答1:</h3><br /><p>As it turns out, the problem was that I had a syntax error in the creation of the Blob.</p> <p>The corrected code looked like: <code>var blob = new Blob([myArr], {type: "octet/stream"});</code></p> <p>I'm not really sure why if I am already passing an ArrayBuffer argument. Why I need bracket notation? Seems redundant?</p> <br /><br /><br /><h3>回答2:</h3><br /><p>According to <strong>Mozilla</strong></p> <p>https://developer.mozilla.org/en-US/docs/Web/API/Blob#Example_for_creating_a_URL_to_a_typed_array_using_a_blob</p> <pre><code>var blob = new Blob([typedArray.buffer], {type: 'application/octet-stream'}); </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/36985406/javascript-save-typed-array-as-blob-and-read-back-in-as-binary-data</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/javascript" hreflang="zh-hans">javascript</a></div> <div class="field--item"><a href="/tag/arrays" hreflang="zh-hans">arrays</a></div> <div class="field--item"><a href="/tag/filereader" hreflang="zh-hans">filereader</a></div> <div class="field--item"><a href="/tag/typed-arrays" hreflang="zh-hans">typed-arrays</a></div> </div> </div> Thu, 18 Feb 2021 02:54:06 +0000 僤鯓⒐⒋嵵緔 4112539 at http://www.e-learn.cn Javascript - Save typed array as blob and read back in as binary data http://www.e-learn.cn/topic/4112536 <span>Javascript - Save typed array as blob and read back in as binary data</span> <span><span lang="" about="/user/226" typeof="schema:Person" property="schema:name" datatype="">泄露秘密</span></span> <span>2021-02-18 10:53:07</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I have a typed array full of binary data that is being generated from an ArrayBuffer</p> <pre><code>var myArr = new Uint8Array(myBuffer); </code></pre> <p>I am presenting this to the user with </p> <pre><code>var blob = new Blob(myArr, {type: "octet/stream"}; var blobURL = URL.createObjectURL(blob); </code></pre> <p>and inserting a link that is</p> <pre><code>"&lt;a href=" + blobUrl + " download=" + filename "/a&gt;" </code></pre> <p>Later, I am letting the user select the file from disk, and using a file reader to do with</p> <pre><code>var reader = new FileReader(); reader.onload = function () { console.log(reader.result); }; reader.readAsArrayBuffer(sourceFile); </code></pre> <p>The problem is, it seems like no matter what I do, I get a "string" of the file's contents. In fact, when I save the file, I can open it, and it is plainly human readable. I.E. if my Uint8Array was <code>{"0" : "51", "1" : "52", "2" : "53" }</code></p> <p>I can open the downloaded blob in a text editor and I just see "515253" which I don't think is what should be happening.</p> <p>How can I make a link to a file download for my file that is formatted properly so I can read it back in an dget the right values?</p> <br /><h3>回答1:</h3><br /><p>As it turns out, the problem was that I had a syntax error in the creation of the Blob.</p> <p>The corrected code looked like: <code>var blob = new Blob([myArr], {type: "octet/stream"});</code></p> <p>I'm not really sure why if I am already passing an ArrayBuffer argument. Why I need bracket notation? Seems redundant?</p> <br /><br /><br /><h3>回答2:</h3><br /><p>According to <strong>Mozilla</strong></p> <p>https://developer.mozilla.org/en-US/docs/Web/API/Blob#Example_for_creating_a_URL_to_a_typed_array_using_a_blob</p> <pre><code>var blob = new Blob([typedArray.buffer], {type: 'application/octet-stream'}); </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/36985406/javascript-save-typed-array-as-blob-and-read-back-in-as-binary-data</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/javascript" hreflang="zh-hans">javascript</a></div> <div class="field--item"><a href="/tag/arrays" hreflang="zh-hans">arrays</a></div> <div class="field--item"><a href="/tag/filereader" hreflang="zh-hans">filereader</a></div> <div class="field--item"><a href="/tag/typed-arrays" hreflang="zh-hans">typed-arrays</a></div> </div> </div> Thu, 18 Feb 2021 02:53:07 +0000 泄露秘密 4112536 at http://www.e-learn.cn How to write multiple ArrayList in a text file http://www.e-learn.cn/topic/4109950 <span>How to write multiple ArrayList in a text file</span> <span><span lang="" about="/user/19" typeof="schema:Person" property="schema:name" datatype="">天大地大妈咪最大</span></span> <span>2021-02-17 07:19:05</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><blockquote> <p>In below code , i'm trying to create a txt file and there i want to add some list of values. So here my approach is create a new file if file is not present and add the respective elements into it.</p> </blockquote> <p>Please see below , i'm unable to get my expected output, So can you help me with some idea so it will be very helpful</p> <pre><code> public class MyTest { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub SftpConn sftp = new SftpConn(); //sftp.sftpGetConnect(); List&lt;String&gt; list = new ArrayList&lt;&gt;(); list.add("AAAA"); list.add("BBBB"); list.add("CCCC"); sftp.writeIntoText(list); List&lt;String&gt; list1 = new ArrayList&lt;&gt;(); list1.add("AAAA1111"); list1.add("BBBB2222"); list1.add("CCCC2222"); sftp.writeIntoText(list1); } } public class SftpConn { public void writeIntoText(List&lt;String&gt; result) throws Exception { connect(); List&lt;String&gt; resultdata= result; System.out.println("Result Updated"); channelSftp = (ChannelSftp) session.openChannel("sftp"); channelSftp.connect(); fileOutStream = channelSftp.put("/home/dasrsoum/RM.txt"); PrintWriter writer = new PrintWriter(fileOutStream,true); writer.println("------------------"); for (String value : resultdata) { writer.println(value+ System.lineSeparator()); System.out.println(value); } writer.close(); } </code></pre> <p>Actual output</p> <pre><code> AAAA1111 BBBB2222 CCCC2222 </code></pre> <p>Excepted OutPut</p> <pre><code> AAAA BBBB CCCC AAAA1111 BBBB2222 CCCC2222 </code></pre> <br /><h3>回答1:</h3><br /><p>You first file is overwritten by the second call to the function</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/63048653/how-to-write-multiple-arraylist-in-a-text-file</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/java" hreflang="zh-hans">java</a></div> <div class="field--item"><a href="/tag/file" hreflang="zh-hans">file</a></div> <div class="field--item"><a href="/tag/arraylist" hreflang="zh-hans">arraylist</a></div> <div class="field--item"><a href="/tag/io" hreflang="zh-hans">io</a></div> <div class="field--item"><a href="/tag/filereader" hreflang="zh-hans">filereader</a></div> </div> </div> Tue, 16 Feb 2021 23:19:05 +0000 天大地大妈咪最大 4109950 at http://www.e-learn.cn FileReader.readAsBinaryString() is not support IE 10, 11 http://www.e-learn.cn/topic/4092628 <span>FileReader.readAsBinaryString() is not support IE 10, 11</span> <span><span lang="" about="/user/47" typeof="schema:Person" property="schema:name" datatype="">谁说胖子不能爱</span></span> <span>2021-02-11 01:43:18</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>The below code works in chrome browser.</p> <pre><code>$('#file').change(function(e) { var fileReader = new FileReader(), file = e.target.files[0]; if (typeof fileReader.readAsBinaryString == "function") { // section #1 var binaryString, base64; fileReader.onload = function(readerEvt) { binaryString = readerEvt.target.result; base64 = 'data:'+type+';base64,'+btoa(binaryString); socket.emit('image', { image: base64, size: file.size, filename: file.name }); } fileReader.readAsBinaryString(file); }else{ // section #2 in IE 10, 11... var binary = "", bytes = e.target.result, length = bytes.length; for (var i=0; i&lt;length; i++) { binary += String.fromCharCode(bytes[i]); } // How can I control 'binary' variable for IE 10, 11 } }); </code></pre> <p>I want to make it work the same as in Google Chrome. </p> <p>Please implementing the source code into section #2.</p> <br /><h3>回答1:</h3><br /><p>All though the documentation says that the <code>readAsBinaryString</code> function is defined, but it still gives <code>Can't be resolved</code> and <code>function definition not found</code> errors.</p> <p>You can try this code. It has worked for me. Please refer my comments for the help. </p> <p>I have tested the code in <strong>IE 11</strong> and <strong>Chrome</strong>.</p> <p><strong>Html Code</strong></p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/jszip.js"&gt;&lt;/script&gt; &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/xlsx.js"&gt;&lt;/script&gt; &lt;script src="index.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;input type="file" id="files" onchange="handleFileSelect(event)"/&gt; &lt;output id="list"&gt;&lt;/output&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>Javascript Code</strong></p> <pre><code>//readAsBinaryString function is not defined in IE //Adding the definition to the function prototype if (!FileReader.prototype.readAsBinaryString) { console.log('readAsBinaryString definition not found'); FileReader.prototype.readAsBinaryString = function (fileData) { var binary = ''; var pk = this; var reader = new FileReader(); reader.onload = function (e) { var bytes = new Uint8Array(reader.result); var length = bytes.byteLength; for (var i = 0; i &lt; length; i++) { var a = bytes[i]; var b = String.fromCharCode(a) binary += b; } pk.content = binary; $(pk).trigger('onload'); } reader.readAsArrayBuffer(fileData); } } function handleFileSelect(evt) { console.log(evt); var reader = new FileReader(); reader.onload = function(e){ if (reader.result) reader.content = reader.result; //In IE browser event object is null var data = e ? e.target.result : reader.content; var baseEncoded = btoa(data); var wb = XLSX.read(baseEncoded, {type: 'base64'}); processWorkbook(wb); }; reader.onerror = function(ex){ console.log(ex); }; //I'm reading the first file //You can modify it as per your need console.log(evt.target.files[0]); reader.readAsBinaryString(evt.target.files[0]); } function processWorkbook(workbook) { console.log(workbook.Sheets['sheet_name']['excel_cell_name_to_be_accessed'].v); //For example console.log(workbook.Sheets['sheet1']['C2'].v); //you can iterate through all the sheets for(var i = 0; i &lt; workbook.SheetNames.length; i++) { workbook.SheetNames[i]['cell_name_to_be_accessed'] //rest of the processing } //You can now work with the workbook as per your requirements } </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/31086243/filereader-readasbinarystring-is-not-support-ie-10-11</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/javascript" hreflang="zh-hans">javascript</a></div> <div class="field--item"><a href="/tag/filereader" hreflang="zh-hans">filereader</a></div> </div> </div> Wed, 10 Feb 2021 17:43:18 +0000 谁说胖子不能爱 4092628 at http://www.e-learn.cn FileReader.readAsBinaryString() is not support IE 10, 11 http://www.e-learn.cn/topic/4092626 <span>FileReader.readAsBinaryString() is not support IE 10, 11</span> <span><span lang="" about="/user/211" typeof="schema:Person" property="schema:name" datatype="">夙愿已清</span></span> <span>2021-02-11 01:43:13</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>The below code works in chrome browser.</p> <pre><code>$('#file').change(function(e) { var fileReader = new FileReader(), file = e.target.files[0]; if (typeof fileReader.readAsBinaryString == "function") { // section #1 var binaryString, base64; fileReader.onload = function(readerEvt) { binaryString = readerEvt.target.result; base64 = 'data:'+type+';base64,'+btoa(binaryString); socket.emit('image', { image: base64, size: file.size, filename: file.name }); } fileReader.readAsBinaryString(file); }else{ // section #2 in IE 10, 11... var binary = "", bytes = e.target.result, length = bytes.length; for (var i=0; i&lt;length; i++) { binary += String.fromCharCode(bytes[i]); } // How can I control 'binary' variable for IE 10, 11 } }); </code></pre> <p>I want to make it work the same as in Google Chrome. </p> <p>Please implementing the source code into section #2.</p> <br /><h3>回答1:</h3><br /><p>All though the documentation says that the <code>readAsBinaryString</code> function is defined, but it still gives <code>Can't be resolved</code> and <code>function definition not found</code> errors.</p> <p>You can try this code. It has worked for me. Please refer my comments for the help. </p> <p>I have tested the code in <strong>IE 11</strong> and <strong>Chrome</strong>.</p> <p><strong>Html Code</strong></p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/jszip.js"&gt;&lt;/script&gt; &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/xlsx.js"&gt;&lt;/script&gt; &lt;script src="index.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;input type="file" id="files" onchange="handleFileSelect(event)"/&gt; &lt;output id="list"&gt;&lt;/output&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>Javascript Code</strong></p> <pre><code>//readAsBinaryString function is not defined in IE //Adding the definition to the function prototype if (!FileReader.prototype.readAsBinaryString) { console.log('readAsBinaryString definition not found'); FileReader.prototype.readAsBinaryString = function (fileData) { var binary = ''; var pk = this; var reader = new FileReader(); reader.onload = function (e) { var bytes = new Uint8Array(reader.result); var length = bytes.byteLength; for (var i = 0; i &lt; length; i++) { var a = bytes[i]; var b = String.fromCharCode(a) binary += b; } pk.content = binary; $(pk).trigger('onload'); } reader.readAsArrayBuffer(fileData); } } function handleFileSelect(evt) { console.log(evt); var reader = new FileReader(); reader.onload = function(e){ if (reader.result) reader.content = reader.result; //In IE browser event object is null var data = e ? e.target.result : reader.content; var baseEncoded = btoa(data); var wb = XLSX.read(baseEncoded, {type: 'base64'}); processWorkbook(wb); }; reader.onerror = function(ex){ console.log(ex); }; //I'm reading the first file //You can modify it as per your need console.log(evt.target.files[0]); reader.readAsBinaryString(evt.target.files[0]); } function processWorkbook(workbook) { console.log(workbook.Sheets['sheet_name']['excel_cell_name_to_be_accessed'].v); //For example console.log(workbook.Sheets['sheet1']['C2'].v); //you can iterate through all the sheets for(var i = 0; i &lt; workbook.SheetNames.length; i++) { workbook.SheetNames[i]['cell_name_to_be_accessed'] //rest of the processing } //You can now work with the workbook as per your requirements } </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/31086243/filereader-readasbinarystring-is-not-support-ie-10-11</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/javascript" hreflang="zh-hans">javascript</a></div> <div class="field--item"><a href="/tag/filereader" hreflang="zh-hans">filereader</a></div> </div> </div> Wed, 10 Feb 2021 17:43:13 +0000 夙愿已清 4092626 at http://www.e-learn.cn Image/Video Preview In Angular http://www.e-learn.cn/topic/4090103 <span>Image/Video Preview In Angular</span> <span><span lang="" about="/user/53" typeof="schema:Person" property="schema:name" datatype="">依然范特西╮</span></span> <span>2021-02-10 16:49:26</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I'm trying to upload image or video in Angular. I wanted to display its preview. I have no problem in previewing the image, my problem is how can i preview the video? Please see this stackblitz link: </p> <p>CLICK HERE</p> <p><br /></p> <blockquote> <p>CODE</p> </blockquote> <pre><code> onSelectFile(event) { if (event.target.files &amp;&amp; event.target.files[0]) { var reader = new FileReader(); reader.readAsDataURL(event.target.files[0]); reader.onload = (event) =&gt; { this.url = event.target.result; } } } </code></pre> <br /><h3>回答1:</h3><br /><p>You can decide if the file is type of video/image and then forward it to suitable html controls</p> <pre><code>&lt;img [src]="url" *ngIf="format==='image' &amp;&amp; url" height="200"&gt; &lt;br/&gt; &lt;video [src]="url" *ngIf="format==='video' &amp;&amp; url" height="200" controls&gt;&lt;/video&gt; &lt;br/&gt; &lt;input type='file' (change)="onSelectFile($event)" /&gt; </code></pre> <pre><code>onSelectFile(event) { const file = event.target.files &amp;&amp; event.target.files[0]; if (file) { var reader = new FileReader(); reader.readAsDataURL(file); if(file.type.indexOf('image')&gt; -1){ this.format = 'image'; } else if(file.type.indexOf('video')&gt; -1){ this.format = 'video'; } reader.onload = (event) =&gt; { this.url = (&lt;FileReader&gt;event.target).result; } } } </code></pre> <p>Updated Stackblitz: https://stackblitz.com/edit/angular-video-or-image-upload-preview-fjsukm</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/58046879/image-video-preview-in-angular</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/angular" hreflang="zh-hans">angular</a></div> <div class="field--item"><a href="/tag/events" hreflang="zh-hans">events</a></div> <div class="field--item"><a href="/tag/angular6" hreflang="zh-hans">angular6</a></div> <div class="field--item"><a href="/tag/angular7" hreflang="zh-hans">angular7</a></div> <div class="field--item"><a href="/tag/filereader" hreflang="zh-hans">filereader</a></div> </div> </div> Wed, 10 Feb 2021 08:49:26 +0000 依然范特西╮ 4090103 at http://www.e-learn.cn Installing FileReaderSync in Angular2 http://www.e-learn.cn/topic/4081640 <span>Installing FileReaderSync in Angular2</span> <span><span lang="" about="/user/236" typeof="schema:Person" property="schema:name" datatype="">南楼画角</span></span> <span>2021-02-08 19:39:14</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>How do I install <code>FileReaderSync</code> in Angular2?</p> <p>It appears in the file <code>node_modules/typescript/lib/lib.webworker.d.ts</code> but not can use it.</p> <p>The <code>FileReader</code> I can use it without having to import anything.</p> <p>I need to do something different with FileReaderSync?</p> <p><code>error TS2304: Cannot find name 'FileReaderSync'.</code></p> <p></p> <br /><h3>回答1:</h3><br /><p>I had the same issue few months ago, the solution was to write custom typings.</p> <p>main.bowser.d.ts</p> <pre><code>interface FileReaderSync { readAsArrayBuffer(blob: Blob): any; readAsBinaryString(blob: Blob): void; readAsDataURL(blob: Blob): string; readAsText(blob: Blob, encoding?: string): string; } declare var FileReaderSync: { prototype: FileReaderSync; new(): FileReaderSync; }; </code></pre> <p>and include them when you bootstrap angular2</p> <pre><code>// &lt;reference path="./main.browser.d.ts" /&gt; </code></pre> <p>I wrote an article about this just now :D http://icode.co/angular2/php/2016/12/21/async-file-streaming-from-JS-to-PHP-WebWorkers.html</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/39807671/installing-filereadersync-in-angular2</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/angular" hreflang="zh-hans">angular</a></div> <div class="field--item"><a href="/tag/typescript" hreflang="zh-hans">typescript</a></div> <div class="field--item"><a href="/tag/filereader" hreflang="zh-hans">filereader</a></div> </div> </div> Mon, 08 Feb 2021 11:39:14 +0000 南楼画角 4081640 at http://www.e-learn.cn Convert Input File to Base64 in Angular 2 without using FileReader In Ionic V2 http://www.e-learn.cn/topic/4076526 <span>Convert Input File to Base64 in Angular 2 without using FileReader In Ionic V2</span> <span><span lang="" about="/user/114" typeof="schema:Person" property="schema:name" datatype="">﹥>﹥吖頭↗</span></span> <span>2021-02-08 08:32:39</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I am having an issue converting the input file object into Base64 in my <strong>ionic v2 app</strong>. The issue occurs specifically when using the onload/onloadend reader as it gets called only occasionally (rarest of rarest to be very specific).</p> <p>I am able to get the file name, size and object when the user clicks on a image file which triggers the attachFile function, but after that when i convert the file object to Base64 using FileReader - onload or onloadend the callback function rarely gets called and the file object is not getting converted to Base64 as a result of this.</p> <p><strong>File input:</strong></p> <p><code>&lt;input #fileInput type="file" accept="image/*" (change)="attachFile($event)"&gt;</code></p> <p><strong>Attach File:</strong></p> <pre><code>attachFile(fileInput: any) { let fileList: FileList = fileInput.target.files; // I can access the selected file name, size and object correctly without any issues console.log("File name = " + fileList[0].name); console.log("File size = " + fileList[0].size); console.log("File = " + fileList[0]); // Converting the file input to base 64 (This rarely works) this.convertFileToBase64AndSet(fileList); } </code></pre> <p><strong>Base64 Conversion:</strong></p> <pre><code>convertFileToBase64AndSet(fileList: FileList) { if(fileList.length &gt; 0) { var reader = new FileReader(); reader.onloadend = (e: Event) =&gt; { console.log(reader.result); } reader.readAsDataURL(fileList[0]); } } </code></pre> <p>I can understand if this doesn't work altogether, but there are some rare instances where the onload/onloadend callback gets called and successfully converts the object to Base64 in my Android phone.</p> <ul><li>None of the callbacks are called including <strong>onerror</strong> unless the rarest of rarest occasions like I mentioned before.</li> <li>The same code in the web browser works perfectly fine and the callbacks successfully gets called and converts the file object to Base64. </li> </ul><p>So this leaves me with very little options and I am unable to find a decent Base64 conversion plugin tailor made for angular 2 as most of the plugins around are for only the old angular. </p> <p>One such plugin for angular had much promise - angular-base64-upload but I am unable to figure it out on how to run it in angular 2 as the instructions are specifically for the old angular.</p> <p>Surely there must be a way to achieve this requirement, any leads would be highly appreciated.</p> <p><strong><em>My requirement is also such that the Base64 conversion plugin shouldn't be dependant upon any file path and should directly convert the file object got from file input selected by the user.</em></strong></p> <p><strong>P.S:</strong> I already saw all the answers in stackoverflow with respect to the FileReader callbacks and none has worked for me so far.</p> <br /><h3>回答1:</h3><br /><p>Check this . https://plnkr.co/edit/PFfebmnqH0eQR9I92v0G?p=preview</p> <p>Template</p> <pre><code>&lt;input type="file" id="filePicker" (change)="handleFileSelect($event)"&gt; </code></pre> <p>Component:</p> <pre><code>handleFileSelect(evt){ var files = evt.target.files; var file = files[0]; if (files &amp;&amp; file) { var reader = new FileReader(); reader.onload =this._handleReaderLoaded.bind(this); reader.readAsBinaryString(file); } } _handleReaderLoaded(readerEvt) { var binaryString = readerEvt.target.result; this.base64textString= btoa(binaryString); console.log(btoa(binaryString)); } </code></pre> <p><strong>Edit :</strong> Regarding this not working in Mobile, it seems to be known issues in ionic. https://github.com/driftyco/ionic-native/issues/505</p> <p>Things you can try</p> <p>Loading zone.js before cordova.js also fixes it.</p> <p>Specific to ionic2 maintain this sequence order.</p> <pre><code>&lt;script src="build/polyfills.js"&gt;&lt;/script&gt; &lt;script src="cordova.js"&lt;/script&gt; </code></pre> <p>as the polyfills.js executes the zone.js.</p> <br /><br /><br /><h3>回答2:</h3><br /><p>when i tried the previous solution the base 64 wasn't complete The html part</p> <pre><code>&lt;input type="file" class="form-control input-lg" #fileInput type="file" accept="image/*" (change)="conImage($event)" name="Image"/&gt; </code></pre> <p>the component side</p> <pre><code>conImage(input) :void{ //this.profileService.conImage().subscribe(convertimage =&gt; this.convertimage = convertimage); if (input.target.files &amp;&amp; input.target.files[0]) { var reader = new FileReader(); //data = data.replace(/['"]+/g, ''); //$('#imageupload').attr('src', e.target.result); reader.onload = function (e) { console.log(e.target.result) //console.log(e) }; reader.readAsDataURL(input.target.files[0]); } } </code></pre> <p>the console.log output gives a full base64 code, when you put the base64 in the src of an image you'll get the image </p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/43733866/convert-input-file-to-base64-in-angular-2-without-using-filereader-in-ionic-v2</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/angular" hreflang="zh-hans">angular</a></div> <div class="field--item"><a href="/tag/ionic-framework" hreflang="zh-hans">ionic-framework</a></div> <div class="field--item"><a href="/tag/ionic2" hreflang="zh-hans">ionic2</a></div> <div class="field--item"><a href="/tag/filereader" hreflang="zh-hans">filereader</a></div> </div> </div> Mon, 08 Feb 2021 00:32:39 +0000 ﹥>﹥吖頭↗ 4076526 at http://www.e-learn.cn FileReader memory leak in Chrome http://www.e-learn.cn/topic/4062057 <span>FileReader memory leak in Chrome</span> <span><span lang="" about="/user/145" typeof="schema:Person" property="schema:name" datatype="">与世无争的帅哥</span></span> <span>2021-02-07 03:51:40</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I have a webpage with file upload functionality. The upload is performed in 5MB chunks. I want to calculate hash for each chunk before sending it to the server. The chunks are represented by Blob objects. In order to calculate the hash I am reading such blob into an ArrayBuffer using a native FileReader. Here is the code:</p> <pre><code>var reader = new FileReader(); var getHash = function (blob, callback) { reader.onloadend = function (e) { var hash = util.hash(e.target.result); callback(hash); }; reader.readAsArrayBuffer(blob); } var processChunk = function (chunk) { if (chunk) { getHash(chunk, function (hash) { util.sendToServer(chunk, hash, function() { // this callback is called when chunk upload is finished processChunk(chunks.shift()); }); }); } } var chunks = file.splitIntoChunks(); // gets an array of blobs processChunk(chunks.shift()); </code></pre> <p>The problem: using the <code>FileReader.readAsArrayBuffer</code> seems to eat up a lot of memory which is not released. So far I tested with a 5GB file on following browsers:</p> <ul><li><p>Chrome 55.0.2883.87 m (64-bit): the memory goes up to 1-2GB quickly and oscillates around that. Sometimes it goes all the way up and browser tab crashes. It can use more memory than the size of read chunks. E.g. after reading 500MB of chunks the process already uses 700MB of memory.</p></li> <li><p>Firefox 50.1.0: memory usage oscillates around 300-600MB</p></li> </ul><p>Code adjustments I have tried - all to no avail:</p> <ul><li>re-using the same <code>FileReader</code> instance for all chunks (as suggested in this question)</li> <li>creating new <code>FileReader</code> for each chunk</li> <li>adding timeout before starting new chunk</li> <li>setting the <code>FileReader</code> and the <code>ArrayBuffer</code> to null after each read</li> </ul><p>The question: is there a way to fix the problem? Is this a bug in the FileReader implementations or am I doing something wrong?</p> <p><strong>EDIT</strong>: Here is a JSFiddle https://jsfiddle.net/andy250/pjt9udeu/</p> <br /><h3>回答1:</h3><br /><p>This is a bug in Chrome on Windows. It is reported here: https://bugs.chromium.org/p/chromium/issues/detail?id=674903</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/41181923/filereader-memory-leak-in-chrome</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/javascript" hreflang="zh-hans">javascript</a></div> <div class="field--item"><a href="/tag/memory" hreflang="zh-hans">memory</a></div> <div class="field--item"><a href="/tag/file-upload" hreflang="zh-hans">file-upload</a></div> <div class="field--item"><a href="/tag/memory-leaks" hreflang="zh-hans">memory-leaks</a></div> <div class="field--item"><a href="/tag/filereader" hreflang="zh-hans">filereader</a></div> </div> </div> Sat, 06 Feb 2021 19:51:40 +0000 与世无争的帅哥 4062057 at http://www.e-learn.cn