0

My Javascript code that is run in client-side needs to read a binary file that is stored in the server. How can I do it for all browsers?

I have found solutions with ActiveXObject - FileSystemObject that are only working in Internet Explorer.

Thanks

6
  • 1
    Are you sure the javascript is server-side? If it is, then why do you care about the browser? Client-side javascipt runs on the browser. Not server-side. Commented May 23, 2012 at 11:15
  • What do you need to parse out of the binary file? Commented May 23, 2012 at 11:17
  • 1
    server side javascript.. you mean classic .asp with javascript (pages started with @Language="JavaScript" or server language default to javascript)? Commented May 23, 2012 at 11:20
  • 1
    If it is server side, then the browser is irrelevant. Commented May 23, 2012 at 11:28
  • Apologies. This was not server-side but client-side. I was confused but what I wanted to say is that the file to be read is in the server, not local. Commented May 23, 2012 at 11:35

2 Answers 2

1
function getXHR(){
    var xhr;
    try{
        xhr = new XMLHttpRequest();
    }catch(e){
        try{
            xhr = new ActiveXObject("MSXML2.XMLHTTP.6.0");
        }catch(e2){
            try{
                xhr = new ActiveXObject("MSXML2.XMLHTTP");
            }catch(e3){}
        }
    }
    return xhr;
}


function getBinaryData(url, callback){
    var xhr = getXHR();
    xhr.open("GET", url, !!callback);
    if(callback){
        xhr.onload = function(){callback(xhr, true)};
        xhr.onerror = function(){callback(xhr, false)};
    }
    xhr.send();
    return callback ? undefined : xhr.responseText;
}

You would then use getBinaryData to get the file. with asynchronous, it will call the callback with arguments the xhr object itself (you would read the responseText property), and whether it was successful. Synchronously, it returns the binary data.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Will this ActiveXObject calls work on Google Chrome? I believe it is only for IE.
Yes, the ActiveXObject is only used in IE. It first tries just doing new XMLHttpRequest(), which is the W3C standard
0

for classic asp server side javascript (from an old document server i have)

as it is server side all browser will download the file, in this case this piece of code was not to give direct access to file and was used after user login check.

Server.ScriptTimeout=500;//this might take some time
var docs_type="application/pdf";
var filename="...";//put your filename here (relative path)
var objStream = Server.CreateObject("ADODB.Stream");

try {
   objStream.Open();
   objStream.Type=1;//binary
  objStream.LoadFromFile(Server.MapPath(filename));
        Response.AddHeader("Content-Length", objStream.Size);
        Response.ContentType=docs_type;//the type of document you are serving 
        Response.AddHeader("Content-Disposition", "attachment; filename=your_filename.pdf");
        while(!objStream.EOS&&Response.IsClientConnected) {
            Response.BinaryWrite(objStream.Read(4*1024*256));
            Response.Flush();
        }
    objStream.Close();
    Response.End();
  } catch(e) {
        Response.Write("Error serving document<br>");
        Response.End();
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.