JavaScript: How to handle an UCS-2 File? - javascript

I have an Javascript (AngularJS) App, and I try to load an language-file, which is converted in "UCS-2 Little Endian".
I have to use this, and are not allowed to convert the file to UTF-8.
Now the problem is, that I can´t read this file in JavaScript.
// console.log(row);
��[�l�a�n�g�_�d�e�_�n�a�m�e�]�,�G�e�r�m�a�n�
Instead this row should be:
[lang_de_name],German
Is there any way to read the file without changing the file? I already tried encodeURIComponent() or escape(), which does not work.
Thank you very much.

Link: http://terenceyim.wordpress.com/tag/ucs2/
What I do in code now:
var csv = UTF8.decode(UTF8.encode(row))
Works.

Related

Generating files in nodejs escape character issue

I have a NestJs project where I am trying to generate js files into the CDN blob storage.
For eg I have this object:
{
"prop1":"${resolvedprop1}",
"prop2":"John's pet"
}
content of the generated js file should look like:
var metadata=()=>JSON.parse('{ "prop1":"${resolvedprop1}", "prop2":"John's pet" }')
Code for generation of the file:
function writeToCDN(metadata){
const content = `var metadata = ()=>JSON.parse('${JSON.stringify(metadata)}')`;
//write content string to a file in CDN BLOB.
}
When I load this file into the browser and execute metadata() I get an error when parsing because the single quote is not escaped.
Important note:
I can not use backtick instead of single quote inside JSON.parse since I am expecting ${} in the prop1 value.
Thank you for your help in advance!

How do I convert html2pdf PDF to base64?

I'm having some trouble getting the html2pdf.js to properly give me a callback so that I can convert it to a base64 string.
I have tried this:
html2pdf().from(el).then(function(pdf) {
// pdf is null when I log this...
console.log(pdf);
}).save();
Along with many other variations using everything from output() to this:
var pdf = new jsPDF();
html2pdf().from(element).set({ pdf: pdf }).toPdf().save();
All to no avail.
I'm currently on v.0.9.0. All that I really need to get is the base64 so I can send that back to the server and attach it to an email - it doesn't really matter to me how I accomplish that but I'm having issues figuring out how to use this callback properly.
I have searched through the documentation and the issues on github.
What is missing is a call to the outputPdf() method. You should also make sure that you have upgraded to the latest version of the html2pdf plugin as older versions did not have this support.
Your new code should look like this:
html2pdf().from(el).outputPdf().then(function(pdf) {
// This logs the right base64
console.log(btoa(pdf));
});
From the documentation:
[outputPdf] Sends type and options to the jsPDF object's output method, and
returns the result as a Promise (use .then to access)
Simply using output() will not return a promise, you must use outputPdf().

Get filename from input file in Javascript

I have noticed that bootstrap is able to parse out and display the top level filename from a file input field when the user selects a file.
<input type="file" id="exampleFile">
JSFiddle: http://jsfiddle.net/na4j7n6y/
My question: Is this functionality exposed for me to use in my own javascript code? Or do I have to parse out the filename once again using my own technique?
I have tried retrieving the filename like so:
$('#exampleFile').val()
// Resolves to 'C:\fakepath\FILENAME' instead of 'FILENAME'
Well, bootstrap is a framework based on html, css and javascript. The javascript part of this framework uses jQuery and not a own library. Then you could try to solve it using a regex with javascript/jquery, for sample:
var fullPath = $("#exampleFile").val();
var filename = fullPath.replace(/^.*[\\\/]/, '');
Take a look here:
http://jsfiddle.net/na4j7n6y/1/
How to get the file name from a full path using JavaScript?
Check this JSFiddle to reference the filepath displayed.
http://jsfiddle.net/67y9tpd4/
HTML
<div id="write"><button type="button" onclick="getAfilepath()">Print Filepath</button></div>
JS
function getAfilepath(){
var afilepath = document.getElementById("uploadFile").value;
document.getElementById("write").innerHTML=afilepath;
}

jquery load html file containing javascript code

When I tried to load an HTML file containing javascript code, the page doesn't display correctly.
Is there a way to load correctly this page?
PS: I used FireFox to test
<div class="mydiv"></div>
var path = myHTMLWithJavaScript.html;
$.get(path, function(data) {
$(".mydiv").load(path);
})
Either use $.get or $(".mydiv").load(path); not both
Also,
var path = myHTMLWithJavaScript.html;
should be (notice double quotes)
var path = "myHTMLWithJavaScript.html";
and make sure that this file is present in the same directory as your page
Since you are tyring to load JavaScript File / Code it is better to use $.getScript. More information can be found here http://api.jquery.com/jQuery.getScript/

Javascript: how to append data to a file

how can i append data to a file using javascript?
i tried to use this code, but i got an error:
var fso = new ActiveXObject("Scripting.FileSystemOject");
var filepath = fso.GetFile("member.txt");
var fileObject = fso.OpenTextFile(filepath, 8);
file.WriteLine(id + "|" + pass);
fileObject.close();
the error is on var fso = new ActiveXObject("Scripting.FileSystemOject");, written: Error: Automation server can't create object
is there any other way to append the file using javascript or the way to fix this? thanks :)
EDIT:
i have doing what's written on this, and it still not working :/
I just realized these in your code:
var fileObject = fso.OpenTextFile(filepath, 8,true);
You'll need the true-argument, if the file does not exist, or you want to overwrite/append it.
var filepath = fso.GetFile("member.txt");// This won't work.
var filepath = "your_filePath"; // Use this instead
var fileObject = fso.OpenTextFile(filepath, 8, true);
OpenTextFile() needs a path as a string like "D:/test/file.txt". GetFile() returns an object, which you can see as a string (D:\test\file.txt), but it's not a string. Use also absolute paths, relative paths don't seem to work by my experience.
EDIT
Add the code below to the <head>-part of your html-file, then save locally as a hta (with file extension hta, not htm or html).
<hta:application
applicationName="MyApp"
id="myapp"
singleInstance="yes"
/>
Then run the hta-file. If you still getting an ActiveX-error, it's not supported by your OS. If this works, you haven't done all the security settings correct.
EDIT II
In this case it's not very usefull to get the path through ActiveX, you'll need to write it literal anyway. And I'm not supposed to do your homeworks, but this does the trick...
var filepath = new String(fso.GetFile("member.txt")).replace(/\\/g,'/');
And don't forget what I've said above about using absolute paths...
The 8 in the OpenTextFile function specify that you want to append to the file. Your problem comes from the security restriction of your browser. To make it work you'll have to lower the security level, which is not really recommended.
The error is thrown because there are security restrictions which donot allow the activex to run. change your security settings to allow the activex if your using internet explorer (which i think you are).
This might be useful http://windows.microsoft.com/en-US/windows/help/genuine/ie-activex
Cheers
EDIT: i have doing what's written on this, and it still not working :/
* try Restarting your browser
As pointed out in this comment
Javascript: how to append data to a file
the cause of the error Error: Automation server can't create object is the typo in the progid passed to ActiveXObject: Oject instead of Object:
var fso = new ActiveXObject("Scripting.FileSystemOject");
there is a missing b!

Categories