forked from nanoboard/nanoboard
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
img2base64.js - changed. 1. Replace bb-code "xmg" to bb-code '[file name="filename.extension" type="mime-type"]BASE64-CONTENT[/file]' Now any file can be uploaded, like on karasiq-nanoboard. Filename and mime-type extracting when file selected. See lines 63, 76 and 122 in img2base64.js 2. Add some comments. 3. The page /pages/download_as_binary.html was been added - to download any base64 or dataURL, as binary file. nanoclient.js - changed. 4. At line 1 was been added the function to check without throw error, is content - base64 encoded or not. 5. At line 106 was been added the cycle for extracting base64, filename and filetype from '[file name="filename.extension" type="mime-type"]BASE64-CONTENT[/file]', and replace this to link for downloading the file. 6. At line 172 was been added the link to download_as_binary.html with base64 encoded png icon. This code can be commented if you don't see any sense with this. 7. Old code saved at line 193. 8. Add comment at line 206. [xmg] - tags still supported and working, but not adding now. See: Karasiq/nanoboard#5 If anyone cann't download any file, you can using download_as_binary.html Just download this, put in 'pages', and open http://127.0.0.1:7346/pages/download_as_binary.html _________________________ Have a nice day.
- Loading branch information
1 parent
95fe9c4
commit 4325a23
Showing
3 changed files
with
170 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<h1>Download base64 or dataURL - as binary file.</h1> | ||
Base64 or dataURL:<br> | ||
<textarea id="base64" rows="5" cols="50" placeholder="Paste here the base64 or dataURL"></textarea> | ||
<br> | ||
filename.extension: <input id="filename" type="text" placeholder="filename.extension" onclick="this.focus();this.select();"> | ||
<button onclick="download_as_binary()">Download as binary...</button> | ||
<br> | ||
<br> | ||
<div id="download_link"></div> | ||
<script> | ||
//Function for checking, without throw error, is str base64-encoded or not. return true/false. | ||
function isBase64(str) { | ||
try { | ||
return btoa(atob(str)) == str; | ||
} catch (err) { | ||
return false; | ||
} | ||
} | ||
|
||
function is_dataURL(str){ | ||
return (str.indexOf('data:')!==-1 && str.indexOf('base64,')!==-1 && isBase64(str.split('base64,')[1])); | ||
} | ||
|
||
function download_as_binary(){ | ||
var result_div = document.getElementById('download_link'); //get div with result link | ||
result_div.innerHTML = ''; //clear this | ||
|
||
var textarea = document.getElementById('base64'); //get textarea | ||
var maybe_base = textarea.value; //get value from this | ||
var filename = document.getElementById('filename').value || undefined; //set filename or undefined | ||
if(typeof filename==='undefined'){ //if still undefined | ||
//generate random filename | ||
var len = 20;/*<--String Length ...*/for(var s = "", rem = 100000000, n = Math.random()*10*rem; s.length < len;){rem=10*rem%n; s+= (rem%10!==0)?rem.toString(36).replace(".", "").substr(0, len-s.length):"";}//document.write("<br>", 's.length: ', s.length, 's: ', s); | ||
filename = s; | ||
} | ||
filename = filename+((filename.indexOf('.')==-1)?'.bin':''); //if filename was generated | ||
|
||
if(maybe_base===''){ | ||
result_div.innerHTML = 'Empty textarea'; | ||
return false; | ||
}else if(maybe_base.trim()===''){ | ||
result_div.innerHTML = 'Empty trimmed textarea'; | ||
return false; | ||
} | ||
if(is_dataURL(maybe_base)){ | ||
maybe_base = maybe_base.split(',')[1]; | ||
} | ||
var attempt = 0; | ||
|
||
while(true){ | ||
if(attempt>=2){ //trimmed textarea value - not base64 | ||
//break; //exit from cycle | ||
result_div.innerHTML = 'Trimmed textarea value - not base64.'; | ||
return false; //exit from function | ||
}else if(isBase64(maybe_base) && attempt<2){ //try without trim textarea value | ||
var result = 'Download <a href="data:application/octet-stream;base64,'+maybe_base+'" download="'+filename+'">'+filename+'</a>'; | ||
result_div.innerHTML = result; | ||
break; | ||
}else if(is_dataURL(maybe_base)){ | ||
maybe_base = maybe_base.split(',')[1]; | ||
}else{ | ||
result_div.innerHTML = 'Base64 not found in textarea value. Try trim this...'; | ||
maybe_base = maybe_base.trim(); | ||
attempt++; | ||
} | ||
} | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4325a23
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BB-code '[FILE name="filename" type="filetype"]BASE-64[/file]' nanoboard#6