Copy link to clipboard
Copied
Using js I need to create a script that converts a local file into a base 64 string. Any ideas how I can achieve this?
I saw a post from about 8 years ago, outdated and it said:
var file = newFile(filePath);
file.encoding = 'BINARY';
However looking through js Illustrator documentation I don't find a method for encoding.
Previous one is ExtendScript function.
In case of CEP, You can use encodin conversion utils.
var result = window.cep.fs.readFile("~/Desktop/testFilepath.jpg");
if (0 == result.err) {
var binDat = result.data;
var data = cep.encoding.convertion.binary_to_b64(binDat);
console.log(data);
}
Copy link to clipboard
Copied
You need to encode yourself.
Here is a sample function you can reference.
function base64(fileNm) {
var startDate = new Date();
$.writeln("Base64 encode start time= " + startDate);
var theFile = new File(fileNm);
theFile.encoding = "binary";
theFile.open("r");
var binStr = theFile.read();
theFile.close();
var flNm = "/tmpfile.txt";
var tmpFile = new File(flNm);
tmpFile.open("w");
szB64 = new Array;
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; // Accepted b64 chars
for (j=0;j<64;j++){
szB64[j] = keyStr.charAt(j);
}
var ecdStr = "";
var counter = 0;
var binLen = binStr.length - (binStr.length % 3);
for (i=0;i<binLen;i+=3) { // Get 3 chars starting at the begining of the binary string
ecdStr += szB64[binStr.charCodeAt (i) >> 2];
ecdStr += szB64[((binStr.charCodeAt (i) & 0x3) << 4) | (binStr.charCodeAt (i+1) >> 4)];
ecdStr += szB64[((binStr.charCodeAt (i+1) & 15) << 2) | (binStr.charCodeAt (i+2) >> 6)];
ecdStr += szB64[binStr.charCodeAt (i+2) & 0x3f];
counter +=4;
if(counter > 75) { // when hit the limit, write the line and reset counter
tmpFile.writeln (ecdStr);
ecdStr = "";
counter = 0;
}
}
switch (binStr.length % 3){
case 2: //padding 1 characters.
ecdStr += szB64[binStr.charCodeAt (i) >> 2];
ecdStr += szB64[((binStr.charCodeAt (i) & 0x3) << 4) | (binStr.charCodeAt (i+1) >> 4)];
ecdStr += szB64[((binStr.charCodeAt (i+1) & 15) << 2) | (binStr.charCodeAt (i+2) >> 6)];
ecdStr += "=";
tmpFile.writeln (ecdStr);
break;
case 1: //padding 2 character.
ecdStr += szB64[binStr.charCodeAt (i) >> 2];
ecdStr += szB64[((binStr.charCodeAt (i) & 0x3) << 4) | (binStr.charCodeAt (i+1) >> 4)];
ecdStr += "=="
tmpFile.writeln (ecdStr);
break;
default:
tmpFile.writeln (ecdStr);
break;
}
tmpFile.close();
var endDate = new Date();
$.writeln("Base64 encode finish time = " + endDate);
}
Copy link to clipboard
Copied
I'm getting the file path like this:
function uploadImage() {
var f = window.cep.fs.showOpenDialog(false, false, "select jpg file...", "", ["jpg"]);
base64(f.data);
}
But I get no response or error. Is the path and file name what I should pass?
Copy link to clipboard
Copied
Previous one is ExtendScript function.
In case of CEP, You can use encodin conversion utils.
var result = window.cep.fs.readFile("~/Desktop/testFilepath.jpg");
if (0 == result.err) {
var binDat = result.data;
var data = cep.encoding.convertion.binary_to_b64(binDat);
console.log(data);
}