Webservice that returns a file to open with javascript. Ops!
<p>I have a problem: I need to create a webservice (in C#) that could be called from javascript, that returns a file that is generated in (it's an excel) and that for his return, in javascript, opens a new window with this file.
 The web service, which calls from javascript, already is created. The problem is, first, to return this file, and second that javascript get (to being possible without managing to record this file to disc) and open it in a new window.</p>

<p>I go several days searching, and the only solution to return a file is like an array of bytes, which, javascript, is not even capable of recognizing it as a file, far from it to open it in another window. I have located, across ActiveX, to open a file on disc (a .txt) with javascript, but it does not good, since first it would have to receive the file and record it, thing that it did not want to do, but though it wanted, I do not know if it is possible to do.</p>

<p>I am grateful to you in advance everything for the given help.
 Greetings.</p>

<p>To extend, this is the code:
<strong>javascript</strong>:</p>

<pre><code>&lt;script type="text/javascript"&gt;
    function exportar(ctrl) {
    var txt = ctrl.innerHTML;
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    xmlHttp.open("post", "http://localhost:65380/HtmlToExcel.asmx/ExportarExcel", true); 
    xmlHttp.onreadystatechange=doUpdate; 
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
    xmlHttp.send("paginaHtml="+txt); 
} 

function doUpdate() { 
    if(xmlHttp.readyState==4)
    { 
        var xmlDoc=xmlHttp.responseXML;
        var resp = xmlHttp.responseText;
        var responseElement=xmlDoc.getElementsByTagName("string")[0];
        var respText=responseElement.firstChild.nodeValue; 

        // Open the window:
        var manej = window.open(respText);
        alert(manej);
        manej.document.body.innerHTML = respText;
    } 
} 
&lt;/script&gt;
</code></pre>

<p><strong>Webservice</strong>:</p>

<pre><code>    public string ExportarExcel(string paginaHtml)
    {
        //return "&lt;% Response.ContentType = \"application/vnd.ms-excel\" %&gt;" + paginaHtml;
        // Generamos un objeto excel Aplicación:
        Microsoft.Office.Interop.Excel.Application Aplic = new Microsoft.Office.Interop.Excel.Application();
        Aplic.Visible = false;
        Microsoft.Office.Interop.Excel.Workbook libro = Aplic.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
        Microsoft.Office.Interop.Excel.Worksheet hoja = (Microsoft.Office.Interop.Excel.Worksheet)libro.Worksheets[1];
        if (hoja == null)
        {
            return null;
        }
        hoja.Rows.Clear();

        // Chequeamos lo que nos viene y lo cargamos en el excel:
        int i = 0;
        int m = 1; int n = 1;

        paginaHtml = paginaHtml.Replace('\r', ' ').Replace('\n', ' ');
        paginaHtml = paginaHtml.Replace("\\r", " ").Replace("\\n", " ");
        paginaHtml = paginaHtml.Replace("  ", " ");
        paginaHtml = paginaHtml.ToLower().Replace("&lt;b&gt;", "").Replace("&lt;/b&gt;", "");
        paginaHtml = paginaHtml.Replace("&gt;&lt;", "&gt; &lt;");

        string[] palabra = paginaHtml.Split(' ');

        while (i &lt; palabra.Length)
        {
           // Code to convert html table to excel file.
        }
        string fichero = "HtmlToExcel-" + DateTime.Now.ToFileTime() + ".xls";
        string ruta = (string)Server.MapPath("~") + fichero;
        hoja.SaveAs(ruta, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel7, Type.Missing, Type.Missing, false, false, false, Type.Missing, Type.Missing, Type.Missing);
        // Cerramos el fichero:
        libro.Close(true, Type.Missing, Type.Missing); // Cierra el libro con cbios.
        hoja = null;
        Aplic.Quit();
        Aplic = null;

        //// Transformamos a byte[]:
        byte[] fExcel = ConvertirFileToByteArray(ruta);

        // Borramos Fichero:
        File.Delete(ruta);

        return Encoding.Unicode.GetString(fExcel);
    }
    public static byte[] ConvertirFileToByteArray(string ruta)
    {

        FileStream fs = new FileStream(ruta, FileMode.Open, FileAccess.Read);
        /*Create a byte array of file stream length*/
        byte[] b = new byte[fs.Length];
        /*Read block of bytes from stream into the byte array*/
        fs.Read(b, 0, System.Convert.ToInt32(fs.Length));
        /*Close the File Stream*/
        fs.Close();

        return b;
    }
</code></pre>

<p>I have to transform an innerHTML of one page to a página excel and later show this page in a new window.
Can you to put on an example ???</p>
