<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Sat, Nov 10, 2018 at 3:41 AM Mattias Gaertner via Pas2js <<a href="mailto:pas2js@lists.freepascal.org" target="_blank">pas2js@lists.freepascal.org</a>> wrote:</div><div dir="ltr">[...]</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Yes, but that is async and interactive, so has little todo with<br>
SaveToFile. Correct?<br></blockquote><div><br></div><div>It doesn't requires user interaction. And it can be easily written in a sync way (I awalys prefer to use async ones, but ...). Consider the following example (please notice it is just to check and test the idea, so it is not cross-browser, but it could be improved using FileSaver.js ideas):</div><div><br></div><div><div><font size="1"><font face="monospace, monospace">// growing buffer logic / error handling was omitted to make the example clear</font></font></div><div><font size="1"><span style="font-family:monospace,monospace">type</span><br></font></div><div><font face="monospace, monospace" size="1"> TBytesStream = class</font></div><div><font face="monospace, monospace" size="1"> private</font></div><div><font face="monospace, monospace" size="1"> FBytes: TBytes;</font></div><div><font face="monospace, monospace" size="1"> public</font></div><div><font face="monospace, monospace" size="1"> constructor Create(const ABytes: TBytes); virtual;</font></div><div><font face="monospace, monospace" size="1"> procedure SaveToFile(const AFileName: TFileName); virtual;</font></div><div><font face="monospace, monospace" size="1"> property Bytes: TBytes read FBytes;</font></div><div><font face="monospace, monospace" size="1"> end;</font></div><div><font face="monospace, monospace" size="1"><br></font></div><div><font face="monospace, monospace" size="1">constructor TBytesStream.Create(const ABytes: TBytes);</font></div><div><font face="monospace, monospace" size="1">begin</font></div><div><font face="monospace, monospace" size="1"> inherited Create;</font></div><div><font face="monospace, monospace" size="1"> FBytes := ABytes;</font></div><div><font face="monospace, monospace" size="1">end;</font></div><div><font face="monospace, monospace" size="1"><br></font></div><div><font face="monospace, monospace" size="1">procedure TBytesStream.SaveToFile(const AFileName: TFileName); assembler;</font></div><div><font face="monospace, monospace" size="1">asm</font></div><div><font face="monospace, monospace" size="1"> if (window.navigator.msSaveOrOpenBlob) // IE10+</font></div><div><font face="monospace, monospace" size="1"> window.navigator.msSaveOrOpenBlob(file, filename);</font></div><div><font face="monospace, monospace" size="1"> else { // Others</font></div><div><font face="monospace, monospace" size="1"> if (!window.Blob)</font></div><div><font face="monospace, monospace" size="1"> throw "The Blob API is not supported in this browser";</font></div><div><font face="monospace, monospace" size="1"> var file = new Blob([this.FBytes], {type: "application/octet-stream"});</font></div><div><font face="monospace, monospace" size="1"> var url = (URL || webkitURL).createObjectURL(file);</font></div><div><font face="monospace, monospace" size="1"> var a = document.createElement("a"),</font></div><div><font face="monospace, monospace" size="1"> url = URL.createObjectURL(file);</font></div><div><font face="monospace, monospace" size="1"> a.href = url;</font></div><div><font face="monospace, monospace" size="1"> a.download = AFileName;</font></div><div><font face="monospace, monospace" size="1"> document.body.appendChild(a);</font></div><div><font face="monospace, monospace" size="1"> a.click();</font></div><div><font face="monospace, monospace" size="1"> setTimeout(function() {</font></div><div><font face="monospace, monospace" size="1"> document.body.removeChild(a);</font></div><div><font face="monospace, monospace" size="1"> window.URL.revokeObjectURL(url);</font></div><div><font face="monospace, monospace" size="1"> }, 0);</font></div><div><font face="monospace, monospace" size="1"> }</font></div><div><font face="monospace, monospace" size="1">end;</font></div><div><font face="monospace, monospace" size="1"><br></font></div><div><font face="monospace, monospace" size="1">var</font></div><div><font face="monospace, monospace" size="1"> b: TBytesStream;</font></div><div><font face="monospace, monospace" size="1">begin</font></div><div><font face="monospace, monospace" size="1"> b := TBytesStream.Create([65, 66, 67]);</font></div><div><font face="monospace, monospace" size="1"> b.SaveToFile('filename.txt');</font></div><div><font face="monospace, monospace" size="1">end.</font></div></div><div><font face="monospace, monospace" size="1"><br></font></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Huh? Of course you still need TJSArrayBuffer for reading binary<br>
data and storing large data.<br></blockquote><div><br></div><div>Do you want to make it for ES6+ only? I think something like this could be used: <font face="monospace, monospace" size="1">{$IF ECMAScript>=6}TJSArrayBuffer{$ELSE}TBytes{$ENDIF}</font>. So ugly, but it is just to show that ES compatibility can be easily solved. Or, in an another way:</div><div><br></div><div><span style="font-family:monospace,monospace;font-size:x-small">procedure TBytesStream.Write(...);</span><br></div><div><span style="font-family:monospace,monospace;font-size:x-small">begin</span></div><div><font face="monospace, monospace" size="1"> CheckES6Plus; // raises something like 'Not implemented in this ES edition'</font></div><div><font face="monospace, monospace" size="1">...</font></div><div><span style="font-family:monospace,monospace;font-size:x-small"><br></span></div><div><font face="monospace, monospace" size="1">procedure TBytesStream.SaveToFile(...);</font></div><div>begin</div><div><span style="font-family:monospace,monospace;font-size:x-small"> </span><span style="font-family:monospace,monospace;font-size:x-small">CheckES6Plus</span><span style="font-family:monospace,monospace;font-size:x-small">;</span><br></div><div><span style="font-family:monospace,monospace;font-size:x-small">...</span></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
I used it for the nodepas2js. Not so sure about if it is awesome. It<br>
seems to start a lot of threads and I missed fast functions for reading<br>
directories. I'm spoiled by fpc.<br></blockquote><div><br></div><div>I would like to test it.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Do you mean the -P parameters like -PECMAScript6?<br>
Works the same:<br>
{$IFDEF ECMAScript6} or {$IF ECMAScript>5}</blockquote></div><div><br></div><div>Perfect! Now I checked it and worked like a charm. It will be very useful in this work ...</div><div><br></div>--<br><div dir="ltr" class="gmail-m_-3772720076596791715gmail_signature"><div dir="ltr"><div>Silvio Clécio</div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>