Generate csv file from javascript with special characters [duplicate] - javascript

This question already has answers here:
How to open a text file with Excel in UTF-8 encoding?
(1 answer)
Replace values across ALL worksheets with new value
(2 answers)
Closed 4 years ago.
This the follow of this topic Here.
Now I can generate and open a csv file. I just have a small problem with special characteres like €...
When I open file with notepad++ : no problem. But under excel I've funny charactere...
Same problem with file generate with IE, Chrome or FF.
In notepad++, say encoding UTF-8. I think Excel try other encoding. Is it possible to force work with utf-8 for csv file or do i change encoding in my codes... I've no idea about good way to take.
UPDATE
I see methods to force Excel to open it in good format, but I need a methods more simple. It's an extract. When you click on button you don't want save on desk, Open Excel->Data... 10 clicks after see the good result.
I search a method more automatic, you click, you see. Hoping is it possible???
UPDATE 2
And solution, but my question is wrongly put to duplicate.
In fact, the solution is to add in javascript generator code a string to force Excel to open in UTF-8 directly. With this one and this other one
I correct my code fir IE>9 and Chrome and FF (other i don't care know)
if(window.navigator.msSaveBlob){
var blob = new Blob([new Uint8Array([0xEF,0xBB,0xBF]),result],{type:'application/csv;charset=UTF-8'});
window.navigator.msSaveBlob(blob,filename);
}else if(window.webkitURL != null){
var a=document.createElement("a");
a.href="data:application/csv;charset=UTF-8,%EF%BB%BF" + encodeURIComponent(result);
a.download=filename;
a.click();
}
Just enjot it :-)

Related

How to pass a string which contain UTF-8 characters from javascript to PHP? [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
How to store other languages (unicode) in cookies and get it back again
(1 answer)
Closed 5 years ago.
I'm building a references generator and I need to keep track of the generated references. To achieve this, I'm currently using Javascript to store a cookie containing the raw data, then a PHP page embedded on the result page takes the cookie and logs it into an HTML page, however, some characters appears like this : �. I really don't know which way to go to solve this (base64 encoding, unicode encoded characters...)
Here's the link to the website : http://louisfelixberthiaume.000webhostapp.com/source_siteweb.php
[It's in french]
I can't give you the link to the HTML page for obvious confidentiality reasons, however I'll provide a screenshot :
Generated references screenshot

Copy Javascript-Code into PHP [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 7 years ago.
I have a problem...
I want to send a Javascript-Document-Code with a Mail-function...
The PHP-Code looks like this:
$script = '<script>
vfprintf(handle, format, args)ar hljs=new function(){function k(v){return v.replace(/&/gm,\"&\").replace(/</gm,\" ect...
</script>';
(It would be too log to post it all, 30.000 characters...)
How can I assure, that the Special-Characers (", ', \, //, ...) are escaped? :s
EDIT:
This is not a duplicate of the thread, please read it carefully!
Code in one language which emits code in another language is a notoriously difficult thing when it comes to escaping "special" characters. When that code gets upward of tens of thousands of characters then it makes a lot more sense to store it in another file which would be appropriate for that language. In this case, a .js file.
That file can be maintained as that language and not as just a literal string in PHP. This gives you things like syntax checking, debugging, testing, etc.
If the end result (such as a web page or some other displayed HTML) can simply refer to that file separately, then host the file somewhere and just send a reference to it (such as a script tag in the case of HTML). The end user's system will download the file accordingly.
If the end result needs to have this content directly embedded, then in PHP you'd read the file's contents into a string and emit that. Something like file_get_contents('yourScript.js').
Treat code as code, not as string literals.

Show file path in firefox/chrome [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Full path from file input using jQuery
I have the following html element
<input type="file" id="upload">
If I use
$("#upload").val();
I only the file name, not the complete absolute path of the file. Can someone tell me how do get the complete path?
You cannot do so - the browser will not allow this because of security concerns. Although there are workarounds, the fact is that you shouldn't count on this working. The following Stack Overflow questions are relevant here:
full path from file input using jquery
How to get the file path from HTML input form in Firefox 3
In addition to these, the new HTML5 specification states that browsers will need to feed a Windows compatible fakepath into the input type="file" field, ostensibly for backward compatibility reasons.
http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2009-March/018981.html
The Mystery of c:\fakepath Unveiled
So trying to obtain the path is worse then useless in newer browsers - you'll actually get a fake one instead.

How to avoid displaying "C:fakepath" in browsers [duplicate]

This question already has answers here:
How to resolve the C:\fakepath?
(13 answers)
Closed 9 years ago.
How to avoid displaying "C:fakepath" while file uploading. While uploading the file javacript method is called to set the selected path im my case
This is a browser security feature that is implemented.
According to the specifications of HTML5, a file upload control should not reveal the real local path to the file you have selected, if you manipulate its value string with JavaScript. Instead, the string that is returned by the script, which handles the file information is c:\fakepath.
If you still want to get rid of this fakepath you may check out this site
How To Get Rid of C:\FakePath in IE When Uploading A File to the Website
Hope it helps.

Squared Question Mark Sign on CSV file read from JS

I'm reading a CSV file in my JS, but characters with accent (á, ó...) are being replaced with a black square question mark (�).
I always have this sort of problem in PHP, but, i'm using JS and i don't know how to fix that.
The problem is in the UTF8 codification of the file, of the HTML, is there a way to fix this in code?
Thanks
This character is U+FFFD, REPLACEMENT CHARACTER, commonly used to replace invalid data in streams thought to be some Unicode encoding.
For example if you had the text "Résumé" encoded as IS0 8859-1 and wanted to convert it to UTF-16, but told the conversion routine that the text was UTF-8 then the library would probably produce the UTF-16 text "R�sum�" (the other alternative would be to throw an error and not give any results).
Another way these may appear is if a web page declares that it is UTF-8 but it is not actually UTF-8. The browser is likely to do the re-encoding described above and the replacement characters will show up in the rendered web-page, but viewing the source with an editor that ignores or disregards the HTML encoding info will show the characters correctly.
From your comments it looks like your process is something like:
Excel -> export to csv -> process csv in js -> produce html
Windows software typically uses the platform's 'encoding for non-Unicode programs' for encoding eight bit text, not UTF-8. So the CSV file is probably Windows CP1252 (If you're using a version of windows set up for most of the western world), and if your javascript program is reading that data and copying it directly into HTML source that's supposed to be UTF-8, that would cause a problem that fits your description.
What you need to do convert from whatever encoding the CSV is using to UTF-8. Javascript doesn't really have the facilities to do this so your best bet is probably to convert the file after exporting it from Excel but before accessing it in JS.
Other alternatives are to change the encoding the HTML page is using to whatever the csv uses, or to not specify an encoding and leave it up to the browser to guess.

Categories

Resources