-2

I have a string which contains data in csv format, and now I want that string to be converted to encoded binary data using javascript and I need to send it to infopath form.

My csv string looks like this:

heading1,heading2,heading3,heading4,heading5
value1_1,value2_1,value3_1,value4_1,value5_1
value1_2,value2_2,value3_2,value4_2,value5_2
Ratbert
  • 5,463
  • 2
  • 18
  • 37
user3756059
  • 21
  • 1
  • 6
  • Could you give an example of what you want the expected output to look like. – bhspencer Apr 17 '15 at 18:43
  • output will be base64 encoded binary – user3756059 Apr 17 '15 at 18:49
  • Could you be more specific? Do you want to encode each value as base64 but still have the values comma separated or do you want to base64 encode the entire csv file as a single base64 string? – bhspencer Apr 17 '15 at 18:54
  • no i dont need just decoding , i need format of my string saving in infopath form – user3756059 Apr 17 '15 at 18:56
  • when i take some sample of code from infopath xml attachment tag and decode it i got result of �IFV�filename.csvheading1,heading2,heading3,heading4,heading5 value1_1,value2_1,value3_1,value4_1,value5_1 value1_2,value2_2,value3_2,value4_2,value5_2 – user3756059 Apr 17 '15 at 19:11
  • for encoding a csv to base64 using C# refer this site https://support.microsoft.com/en-us/kb/2517906 i need the same output using javascript – user3756059 Apr 17 '15 at 19:20

1 Answers1

2

part one... the first google result for "js string to binary" is How to convert text to binary code in JavaScript?

and for the second part (base64 encode) btoa(stringToEncode);

EDIT:

As mentioned by bhspencer in the comments, btoa has inconsistent browser support, specifically pre ie9 that I know of... a better option is to use a library. the one he mentions https://github.com/dankogai/js-base64 is actually the same one I use on a few production sites

Community
  • 1
  • 1
Trey
  • 5,480
  • 4
  • 23
  • 30
  • 1
    I believe btoa use is still discouraged due to inconsistent browser support. You should use a base64 library instead such as this one https://github.com/dankogai/js-base64 – bhspencer Apr 17 '15 at 18:57
  • when i take some sample of code from infopath xml attachment tag and decode it i got result of �IFV�filename.csv – user3756059 Apr 17 '15 at 19:06
  • when i take some sample of code from infopath xml attachment tag and decode it i got result of �IFV�filename.csvheading1,heading2,heading3,heading4,heading5 value1_1,value2_1,value3_1,value4_1,value5_1 value1_2,value2_2,value3_2,value4_2,value5_2 – user3756059 Apr 17 '15 at 19:07
  • for encoding a csv to base64 using C# refer this site support.microsoft.com/en-us/kb/2517906 i need the same output using javascript – user3756059 Apr 17 '15 at 19:23
  • so btoa is not the exact thing for encoding an attachment – user3756059 Apr 17 '15 at 19:24
  • Javascript != C#, your question says "I have a string" which can be converted to binary with the first part of my answer, and the library link that has been added can convert the binary to Base64. I'm not sure what else you'd like from us, and I'm not sure what you mean by "attachment" – Trey Apr 17 '15 at 19:26
  • if you execute the code giving csv file path to that C# code , then you will get the output same but with filename as below �IFV�filename.csvheading1,heading2,heading3,heading4,heading5 value1_1,value2_1,value3_1,value4_1,value5_1 value1_2,value2_2,value3_2,value4_2,value5_2 now my csv string contains only heading1,heading2,heading3,heading4,heading5 value1_1,value2_1,value3_1,value4_1,value5_1 value1_2,value2_2,value3_2,value4_2,value5_2 here the challenge is how to join the file name to my string as above, it contains some ascii values also – user3756059 Apr 17 '15 at 19:37