Archive

Posts Tagged ‘HTML’

HTML & JScript – Read and Encode file

We got a requirement in ADX portal, to read and convert the file content to Base64String , which eventually will be submitted to an external API.

Below is the code snippet to read the File content, browsed using the HTML ‘File Upload’ control:

function readFileAsText(){
try {
// Read the browsed file from ‘File Upload’ control.
var fileToLoad = document.getElementById(“fileOE“).files[0];

var fileReader = new FileReader();
fileReader.onload = function (fileLoadedEvent) {
// Set the file content to variable
var textFromFileLoaded = fileLoadedEvent.target.result;
alert(“File Content – ” + textFromFileLoaded);

// Using ‘window.btoa’ encode the plain text to Base 64
var convertedString = window.btoa(unescape(encodeURIComponent(textFromFileLoaded)));
alert(“Base 64 content – ” + convertedString);
};

fileReader.readAsText(fileToLoad, “UTF-8”);
} catch (e) {
alert(“Error in readFileAsText(); Error – ” + e.description);
}
}

<body>
<label for=”fileOE”>Pick the file </label><br>
<input type=”file” id=”fileOE” />
<input type=”button” value=”Upload” onclick=”readFileAsText()” />
</body>

  • Execute the code and you will get output as below
jScript_1

File content as text

jScript_2

Encoded File Content (Base 64)

🙂

Categories: Misc Tags: , , , ,

Network Error while accessing external service from CRM web resource

October 28, 2014 Leave a comment

We had a requirement to fetch and display data from external hosted service, on a HTML web resource  hosted in CRM.

So I created a HTML file with below logic to fetch data from external hosted service.

var xmlhttp = new XMLHttpRequest();
xmlhttp.open(‘POST’, {External Service URL}, false);

xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
// Success
}
}
}

The logic was working fine, when I tested individual HTML fine.

But when hosted in CRM, I was getting “Network Error”.

Reason :

  • Cross domain issue while accessing HTML from CRM

Fix :

  • Setting “Access data sources across domains” to “true” in IE browser solved the issue
Access data sources across domains

Access data sources across domains

🙂