|
1 | 1 |
|
2 | 2 |
|
| 3 | +<!DOCTYPE html> |
3 | 4 |
|
4 | 5 | <html> |
5 | 6 |
|
6 | 7 | <head> |
| 8 | + <meta charset=utf-8> |
7 | 9 |
|
8 | | - <title>base64 encode file</title> |
| 10 | + <title> |
| 11 | + Base64 Encode File |
| 12 | + </title> |
| 13 | + |
9 | 14 | <meta name=description content='base64 encode files, optional dataurl'> |
10 | 15 |
|
11 | | - <base href='https://ext-code.com/utils/misc/base64-encode-file/'> |
12 | | - <base href='https://javascript-2020.github.io/utils/misc/base64-encode-file/base64-encode-file.html'> |
13 | | - <link rel=canonical href='https://ext-code.com/utils/misc/base64-encode-file/base64-encode-file.html'> |
14 | | - |
15 | | - <meta name=viewport content='width=device-width, initial-scale=1'> |
16 | 16 | <link rel=icon type='image/png' href='image/base64-30.png'> |
| 17 | + <meta name=viewport content='width=device-width, initial-scale=1'> |
17 | 18 |
|
| 19 | + <base href='https://ext-code.com/utils/misc/base64-encode-file/'> |
| 20 | + <link rel=canonical href='https://ext-code.com/utils/misc/base64-encode-file/base64-encode-file.html'> |
18 | 21 |
|
19 | | - |
20 | 22 | <script type='application/ld+json'> |
21 | 23 | { |
22 | 24 | "@context" : "https://schema.org", |
|
33 | 35 | } |
34 | 36 | </script> |
35 | 37 |
|
36 | | - |
37 | 38 |
|
38 | 39 | <script src='https://libs.ext-code.com/js/dom/component/component.js'></script> |
39 | 40 |
|
|
42 | 43 | console.log('base64-encode-file-v2.0.html'); |
43 | 44 | console.log(); |
44 | 45 | console.json=v=>console.log(JSON.stringify(v,null,4)); |
45 | | - var df=true,did='base64-encode-file' |
| 46 | + var df=true,version='v2.0' |
46 | 47 | ; |
47 | 48 |
|
48 | 49 | var ext; |
|
68 | 69 | async function init(){ |
69 | 70 |
|
70 | 71 | //keydown = keydown(); |
71 | | - menu = mod.menumod(); |
| 72 | + menu = menumod(); |
72 | 73 |
|
73 | 74 | hdr = mod['hdr-base64']; |
74 | 75 | //mod.menu.main = mod.menu.main(); |
75 | | - preview.filemod = mod['filemod[preview]']; |
76 | | - preview.filename = mod['filename-ui[preview]']; |
77 | | - output.filemod = mod['filemod[output]'] |
78 | | - output.filename = mod['filename-ui[output]']; |
79 | | - log = mod.log; |
| 76 | + preview.filemod = mod['file-preview']; |
| 77 | + preview.filename = mod['fn-preview']; |
| 78 | + output.filemod = mod['file-output'] |
| 79 | + output.filename = mod['fn-output']; |
| 80 | + log = mod['log-mod']; |
80 | 81 |
|
81 | 82 | hdr.initmod({ext,$,menu,menumod}); |
82 | 83 | //mod.menu.main.initmod({ext,$,menumod,editor,filemod}); |
|
135 | 136 | <style id=page-inline> |
136 | 137 |
|
137 | 138 | html |
138 | | - {height:100%} |
| 139 | + {height:100%;font-family:arial} |
139 | 140 | body |
140 | | - {height:calc(100% - 40px);display:flex;flex-direction:column;gap:10px} |
| 141 | + {min-height:calc(100% - 40px);display:flex;flex-direction:column;gap:20px;margin:20px;align-items:center; |
| 142 | + padding-bottom:200px; |
| 143 | + } |
| 144 | + body>* |
| 145 | + {max-width:1400px;width:100%;padding:0 20px;} |
141 | 146 |
|
142 | 147 |
|
143 | 148 | .icon |
|
170 | 175 | #blob label |
171 | 176 | {width:100px;text-align:right;margin-right:20px} |
172 | 177 |
|
| 178 | + #encode-root |
| 179 | + {display:flex} |
173 | 180 | #dataurl-root |
174 | | - {margin-left:20px;display:inline-flex;align-items:center} |
175 | | - |
| 181 | + {margin-left:20px;display:inline-flex;align-items:center;border:1px solid lightgray;border-radius:5px;padding:5px 10px} |
176 | 182 | #dataurl |
177 | 183 | {width:20px;height:20px;margin:0;margin-left:10px} |
| 184 | + #pretty-root |
| 185 | + {margin-left:20px;display:inline-flex;align-items:center;border:1px solid lightgray;border-radius:5px;padding:5px 10px} |
| 186 | + #pretty-len |
| 187 | + {width:50px;margin-left:10px} |
| 188 | + #pretty |
| 189 | + {width:20px;height:20px;margin:0;margin-left:10px} |
| 190 | + |
178 | 191 |
|
179 | 192 | #b64 |
180 | 193 | {border:1px solid lightgray;padding:10px;flex:1;} |
|
183 | 196 |
|
184 | 197 |
|
185 | 198 | #output |
186 | | - {display:flex;flex-direction:column;gap:10px;border:1px solid lightgray;padding:10px} |
| 199 | + {display:flex;flex-direction:column;gap:10px;border:1px solid lightgray;padding:10px;margin-top:50px} |
187 | 200 | #output-hdr |
188 | 201 | {display:flex;align-items:center} |
189 | 202 | #output .user |
|
208 | 221 |
|
209 | 222 | <hdr-base64 component=grp v2.0> |
210 | 223 | <img class=title src='image/base64-file-50.png' style='top:5px;height:40px'> |
211 | | - <span slot=date>03 Aug 2025</span> |
| 224 | + <time slot=date datetime=2025-10-18>18 Oct 2025</time> |
212 | 225 | </hdr-base64> |
213 | 226 |
|
214 | 227 |
|
215 | 228 |
|
216 | | - <div id=hdr> |
217 | | - |
218 | | -<!-- |
219 | | - <main-menu-page> |
220 | | - <script src='https://html-loader-1024713184986.us-central1.run.app/'></script> |
221 | | - </main-menu-page> |
222 | | ---> |
223 | | - |
224 | | - </div> |
225 | | - |
226 | | - |
227 | 229 | <div id=preview> |
228 | 230 |
|
229 | 231 | <div> |
|
268 | 270 | </div> |
269 | 271 |
|
270 | 272 |
|
271 | | - <div> |
| 273 | + <div id=encode-root> |
272 | 274 | <input value=encode type=button> |
273 | 275 | <span id=dataurl-root> |
274 | 276 | dataurl |
275 | 277 | <input id=dataurl type=checkbox> |
276 | 278 | </span> |
| 279 | + <span id=pretty-root> |
| 280 | + pretty string |
| 281 | + <input id=pretty-len value=140> |
| 282 | + <input id=pretty type=checkbox> |
| 283 | + </span> |
277 | 284 | </div> |
278 | 285 |
|
279 | 286 |
|
|
412 | 419 | return; |
413 | 420 | } |
414 | 421 |
|
415 | | - var dataurl = preview.dataurl.checked; |
416 | | - cur.b64 = await encode(cur.blob,dataurl); |
| 422 | + cur.b64 = await encode(cur.blob); |
417 | 423 |
|
418 | 424 | b64.display(); |
419 | 425 |
|
|
488 | 494 | //: |
489 | 495 |
|
490 | 496 |
|
491 | | - async function encode(blob,dataurl){ |
| 497 | + async function encode(blob){ |
492 | 498 |
|
493 | 499 | var b64 = await blob_b64(blob); |
494 | | - if(dataurl){ |
495 | | - |
496 | | - //var mime = getmime(blob.name); |
497 | | - var mime = blob.type; |
498 | | - b64 = `data:${mime};base64,${b64}`; |
499 | | - } |
500 | 500 | return b64; |
501 | 501 |
|
502 | 502 | }//encode |
|
527 | 527 |
|
528 | 528 |
|
529 | 529 |
|
530 | | - b64.display = function(){ |
| 530 | + b64.display = async function(){ |
531 | 531 |
|
532 | 532 | var str = '---'; |
| 533 | + |
533 | 534 | if(cur.b64){ |
534 | 535 | str = cur.b64; |
| 536 | + |
| 537 | + var dataurl = preview.dataurl.checked; |
| 538 | + if(dataurl){ |
| 539 | + |
| 540 | + var mime = 'application/octet-stream'; |
| 541 | + if(cur.blob){ |
| 542 | + if(cur.blob.type){ |
| 543 | + mime = cur.blob.type; |
| 544 | + }else{ |
| 545 | + if(blob.name){ |
| 546 | + mime = getmime(blob.name); |
| 547 | + } |
| 548 | + } |
| 549 | + } |
| 550 | + str = `data:${mime};base64,${str}`; |
| 551 | + } |
| 552 | + |
| 553 | + var pretty = $('#pretty').checked; |
| 554 | + if(pretty){ |
| 555 | + var txt = str; |
| 556 | + var s = $('#pretty-len').value; |
| 557 | + var len = Number(s); |
| 558 | + if(isNaN(len)){ |
| 559 | + log.red('invalid pretty lenth'); |
| 560 | + return; |
| 561 | + } |
| 562 | + |
| 563 | + var lines = []; |
| 564 | + var n = txt.length; |
| 565 | + for(var i=0;i<n;i+=len){ |
| 566 | + |
| 567 | + var line = txt.slice(i,i+len); |
| 568 | + lines.push(line); |
| 569 | + |
| 570 | + }//for |
| 571 | + lines = lines.map((line,i)=>{ |
| 572 | + |
| 573 | + line = "'"+line+"'"; |
| 574 | + if(i+1<lines.length){ |
| 575 | + line = line.padEnd(len+5); |
| 576 | + line += '+'; |
| 577 | + }else{ |
| 578 | + line += ';'; |
| 579 | + } |
| 580 | + return line; |
| 581 | + |
| 582 | + }); |
| 583 | + //lines.forEach(line=>console.log(line)); |
| 584 | + str = lines.join('\n'); |
| 585 | + } |
| 586 | + |
535 | 587 | } |
536 | 588 |
|
537 | 589 | $(b64.root,'#text').textContent = str; |
538 | 590 |
|
| 591 | + if(str!='---'){ |
| 592 | + var err; |
| 593 | + try{ |
| 594 | + |
| 595 | + await navigator.clipboard.writeText(str); |
| 596 | + |
| 597 | + } |
| 598 | + catch(err2){ |
| 599 | + |
| 600 | + err = err2; |
| 601 | + |
| 602 | + }//catch |
| 603 | + if(err){ |
| 604 | + log.red(err.message); |
| 605 | + return; |
| 606 | + } |
| 607 | + |
| 608 | + log.green('copied to clipboard'); |
| 609 | + } |
| 610 | + |
539 | 611 | }//display |
540 | 612 |
|
541 | 613 |
|
|
0 commit comments