Webservice that returns a file to open with javascript. Ops!
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.
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.
I am grateful to you in advance everything for the given help. Greetings.
To extend, this is the code: javascript:
<script type="text/javascript">
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;
}
}
</script>
Webservice:
public string ExportarExcel(string paginaHtml)
{
//return "<% Response.ContentType = \"application/vnd.ms-excel\" %>" + 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("<b>", "").Replace("</b>", "");
paginaHtml = paginaHtml.Replace("><", "> <");
string[] palabra = paginaHtml.Split(' ');
while (i < 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;
}
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 ???
You can't use javascript to save files to the local disk, unless you use HTML 5 local storage.
Why do you need to "open the file in a new window"? If you simply return the file as a file from your webservice with the correct content-type, the browser will prompt to save/open it.
Can you open a new window using javascript, and use the Querystring of the URL of that window to access your web service?
When you want the browser to show the file in a new window or save it as a download, the browser has to send a new request to the server.
When you received that file via javascript (asynchronous) you can't tell the browser to request it from the page in a new window. You have the data inside a loaded page on the client. Just open a new window with javascript on the client and request the file you want to show via HTTP.
精彩评论