I have a ASP Web Application, as simple as this;
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_Default" %>
<%@ Import Namespace="System.IO" %>
<HTML>
<HEAD>
<script type="text/javascript">
function setHeader(obj)
{
//var fileObj = obj.getAttribute('href').toString();
//var file = fileObj.substring(fileObj.lastIndexOf('/') + 1);
_Default.otro(obj);
//mywindow = window.open ("ejemplo.pdf","mywindow","location=1,status=1, scrollbars=1, resizable=1, width=1500");
}
</script>
</HEAD>
<body>
<form id="Form1" runat="server">
<a id="ejemploHTML" onclick="setHeader('C:\\var\\ejemplo.pdf')">HTML 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_Click(object sender, EventArgs e)
{
//Server.HtmlEncode(Request.ApplicationPath);
//string path = Server.MapPath(Request.ApplicationPath);
//string path = @"ejemplo.pdf";
FileStream MyFileStream = new FileStream(@"C:\ejemplo.pdf", FileMode.Open);
long FileSize;
FileSize = MyFileStream.Length;
byte[] Buffer = new byte[(int)FileSize];
MyFileStream.Read(Buffer, 0, (int)MyFileStream.Length);
MyFileStream.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=MyPDF.PDF");
Response.BinaryWrite(Buffer);
}
</script>
What do you think guys. I hope I explained this clearly.
Thank you in advance.