I have a ASP Web Application, as simple as this;
<%@ Page Language="C#" AutoEventWireup ="true" CodeBehind="Def ault.aspx.cs" Inherits="_Defa ult" %>
<%@ Import Namespace="Syst em.IO" %>
<HTML>
<HEAD>
<script type="text/javascript">
function setHeader(obj)
{
//var fileObj = obj.getAttribut e('href').toStr ing();
//var file = fileObj.substri ng(fileObj.last IndexOf('/') + 1);
_Default.otro(o bj);
//mywindow = window.open ("ejemplo.pdf", "mywindow","loc ation=1,status= 1, scrollbars=1, resizable=1, width=1500");
}
</script>
</HEAD>
<body>
<form id="Form1" runat="server">
<a id="ejemploHTML " onclick="setHea der('C:\\var\\e jemplo.pdf')">H TML Link</a><br />
<br />
</form>
</body>
</HTML>
As you can see.. I call a function on my anchor tag sending a path thought it. Then the JS functon call a function on the code-behind, which looks like this:
Expand|Select|Wrap|Line Numbers
- [AjaxPro.AjaxMethod]
- public void codigoJavaScript()
- {
- MessageBox.Show("Se ejecuta esta funcion desde C# a traves de JS");
- }
- [AjaxPro.AjaxMethod]
- public void setHeaderCB(string pathRelativo)
- {
- try
- {
- string fileName = pathRelativo.Substring(pathRelativo.LastIndexOf('\\') + 1);
- FileStream MyFileStream = new FileStream(pathRelativo, FileMode.Open);
- long FileSize;
- FileSize = MyFileStream.Length;
- byte[] Buffer = new byte[(int)FileSize];
- MyFileStream.Read(Buffer, 0, (int)MyFileStream.Length);
- MyFileStream.Close();
- //System.Web.HttpContext.Current.Response.ContentType = "application/pdf";
- System.Web.HttpContext.Current.Response.AddHeader("content-type", "application/pdf");
- System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
- System.Web.HttpContext.Current.Response.BinaryWrite(Buffer);
- //Necesario, de lo contrario el Response.End generara un error
- System.Web.HttpContext.Current.Response.Clear();
- //System.Web.HttpContext.Current.Response.Flush();
- //System.Web.HttpContext.Current.Response.End();
- }
- catch (Exception e)
- {
- MessageBox.Show(e.ToString());
- }
- HttpContext.Current.Response.End();
- }
I compiled the project and I got all the way to the end of the method and it suppous to prompt a open/save dialog after I call the ByteWriter object, but it's not working despite I dont get any errror.
All this has a different behivor because it's and AJAX Metthod. What else can I use to call a C# code-behind method from JavaScript?
Expand|Select|Wrap|Line Numbers
- protected void Page_Load(object sender, EventArgs e)
- {
- AjaxPro.Utility.RegisterTypeForAjax(typeof(_Default));
- //Establecer el ContentType adecuado.
- Response.ContentType = "Application/pdf";
- Response.AddHeader("Content-Disposition", "attachment;filename=MyPDF.pdf");
- //Get the physical path to the file.
- string FilePath = MapPath("//bin//ejemplo.pdf");
- //Escribir el archivo directamente en la secuencia de contenido HTTP.
- Response.WriteFile(FilePath);
- Response.End();
- }
Also, I add a onclick method to the Default.aspx file and it also works but like I said, it is useless for me because I need to send the parameter thought my anchor tag and that onclick event must be call from an ASP object not Anchor Tag and I need the anchor one.
It looks like this.
<script type="text/C#" runat="server">
public void ejemploASP_Clic k(object sender, EventArgs e)
{
//Server.HtmlEnco de(Request.Appl icationPath);
//string path = Server.MapPath( Request.Applica tionPath);
//string path = @"ejemplo.pd f";
FileStream MyFileStream = new FileStream(@"C: \ejemplo.pdf", FileMode.Open);
long FileSize;
FileSize = MyFileStream.Le ngth;
byte[] Buffer = new byte[(int)FileSize];
MyFileStream.Re ad(Buffer, 0, (int)MyFileStre am.Length);
MyFileStream.Cl ose();
Response.Conten tType = "applicatio n/pdf";
Response.AddHea der("content-disposition", "attachment ; filename=MyPDF. PDF");
Response.Binary Write(Buffer);
}
</script>
What do you think guys. I hope I explained this clearly.
Thank you in advance.