none
Cannot read contents of a text file in HTML5/JavaScript in IE7+ on Azure.

    Question

  • Hi

    I have to create an HTML page for a Windows Azure application where the user can select a file (.CSV particularly) from the file uploader and read its contents, analyze the contents and then upload the analyzed information to the Azure application. I have to do all this in JavaScript only. No server code involved.

    Second thing is that the users of the application shall only be allowed to use only IE7+, no other browser.

    Here is my code...

     

    <!DOCTYPE HTML>
    <html lang="en">
    <head>
    <title>Reading File</title>
    <script>
     function ReadFile() {
      var file = document.getElementById('file');
      // Read file in Internet Explorer
      if (checkBrowser("MSIE")) {
       var forReading = 1, forWriting = 2, forAppending = 8;
       if (file.value != "") {
        name = file.value.replace("\\", "\\\\");
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        if (fso.FileExists(name)) {
         f = fso.GetFile(name)
         if (f.size) {
          if (f.Type == "CSV File") {
           f = fso.OpenTextFile(name, forReading);
           if (f) {
            var txt = f.ReadAll();
            alert(txt);
            f.close
           }
           else
            alert("Error while opening the file");
          }
          else
           alert("Only CSV files are supported");
         }
         else
          alert("File is empty");
        }
        else
         alert("File Not Found");
       }
       else
        alert("No file selected to read");
      }
     }
    
     function checkBrowser(browserName) { return navigator.userAgent.indexOf(browserName); }
    </script>
    </head>
    <body>
     <input type="file" id="file" />
     <br />
     <input type="button" onclick="ReadFile();" value="Read File" />
    </body>
    </html>
    
    

     

     

    The problem with the code runs perfectly outside the web-server (as a simple web-page, though we have to allow ActiveX in the IE browser every time a request is made to read the file), but inside and Web application and Azure application, it just does not work.

     

    In Web Application, on the line var fso = new ActiveXObject("Scripting.FileSystemObject"); I get the error message

     Microsoft JScript runtime error: Automation server can't create object

     

    In the Azure Application, on the line for (i = 0; i < files.length; i++), I get the error message

         Microsoft JScript runtime error: 'length' is null or not an object

     

    I know it's all about ActiveX in IE. 

    Is there ever a way to run this code in .HTML page in IE7+ in Azure?

     

    Any help please?

     


    Kumar Kush (Impetus Infotech (India) Pvt. Ltd.)
    Friday, May 27, 2011 5:00 AM

Answers

  • Hi Kumar,

    Unfortunately you can't bypass this procedure if you want to invoke ActiveX controls in browser. Other alternative would be to implement this functionality in server side code.

    Hope this helps.

    Thanks

    Gaurav

    • Marked as answer by KumarKush Friday, May 27, 2011 7:11 AM
    Friday, May 27, 2011 6:44 AM

All replies

  • Hi Kumar,

    Basically IE's security settings is preventing you for invoking ActiveX objects. You would need to add your website under "Trusted Sites" in IE and the configure it accordingly. To add a site into trusted site, Click on Tools --> Internet Options --> Security Tab --> Trusted Sites --> Sites button. After that you would need to click on "Custom Level" button for finer control (like allowing ActiveX controls settings etc.).

    Hope this helps.

    Thanks

    Gaurav Mantri

    Cerebrata Software

    http://www.cerebrata.com

     

     

    Friday, May 27, 2011 6:26 AM
  • Hi Gaurav, thanks for reply

    I have done that. Works for me. But I cannot expect every user to do the same thing. May be done as a last resort.

    But can't we bypass this procedure or automate this through code?


    Kumar Kush (Impetus Infotech (India) Pvt. Ltd.)
    Friday, May 27, 2011 6:37 AM
  • Hi Kumar,

    Unfortunately you can't bypass this procedure if you want to invoke ActiveX controls in browser. Other alternative would be to implement this functionality in server side code.

    Hope this helps.

    Thanks

    Gaurav

    • Marked as answer by KumarKush Friday, May 27, 2011 7:11 AM
    Friday, May 27, 2011 6:44 AM