473,325 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Prompt Open/Save dialog box in code-behind and javascript

I guys, I got 2 days trying to go thought this and I cannot figure out how to solve it, hope you can help me out here.

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
  1. [AjaxPro.AjaxMethod]
  2.         public void codigoJavaScript()
  3.         {
  4.             MessageBox.Show("Se ejecuta esta funcion desde C# a traves de JS");
  5.         }
  6.  
  7.         [AjaxPro.AjaxMethod]
  8.         public void setHeaderCB(string pathRelativo)
  9.         {
  10.             try
  11.             {
  12.                 string fileName = pathRelativo.Substring(pathRelativo.LastIndexOf('\\') + 1);
  13.                 FileStream MyFileStream = new FileStream(pathRelativo, FileMode.Open);
  14.                 long FileSize;
  15.                 FileSize = MyFileStream.Length;
  16.                 byte[] Buffer = new byte[(int)FileSize];
  17.                 MyFileStream.Read(Buffer, 0, (int)MyFileStream.Length);
  18.                 MyFileStream.Close();
  19.                 //System.Web.HttpContext.Current.Response.ContentType = "application/pdf";
  20.                 System.Web.HttpContext.Current.Response.AddHeader("content-type", "application/pdf");
  21.                 System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
  22.                 System.Web.HttpContext.Current.Response.BinaryWrite(Buffer);
  23.                 //Necesario, de lo contrario el Response.End generara un error
  24.                 System.Web.HttpContext.Current.Response.Clear();
  25.                 //System.Web.HttpContext.Current.Response.Flush();                
  26.                 //System.Web.HttpContext.Current.Response.End();
  27.             }
  28.             catch (Exception e)
  29.             {
  30.                 MessageBox.Show(e.ToString());
  31.             }
  32.             HttpContext.Current.Response.End();
  33.  
  34.         }
First of all I tried to use simply the sentence Response object without the whole referece, then I got some troubles using this object, because it's and AJAX method. I had to add the whole System.Web.HttpContext.Current.* to be able to use this object.

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
  1. protected void Page_Load(object sender, EventArgs e)
  2.         {
  3.             AjaxPro.Utility.RegisterTypeForAjax(typeof(_Default));
  4.             //Establecer el ContentType adecuado.
  5.             Response.ContentType = "Application/pdf";
  6.             Response.AddHeader("Content-Disposition", "attachment;filename=MyPDF.pdf");
  7.             //Get the physical path to the file.
  8.             string FilePath = MapPath("//bin//ejemplo.pdf");
  9.             //Escribir el archivo directamente en la secuencia de contenido HTTP.
  10.             Response.WriteFile(FilePath);
  11.             Response.End();
  12.         }

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.
Jul 2 '10 #1
0 2811

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Ronnie Jensen | last post by:
Hi There. Unfortunally I forgot how to create an open/save dialog (Access 2000), from which I can retrieve the the filename to open/save. Can anyone tell me how.. Thanks in advance Ronnie
1
by: ASH | last post by:
Hello all, I'm using the "API: Call Windows File Open/Save Dialog Box" to source the name of a file to open. The Dialog Box when called opens in the top left hand corner of the screen. I have...
2
by: SpotNet | last post by:
Hi Newsgroup, I have constructed highly customisable common dialog classes\objects. Decided to extend the Open\Save dialog options with the ability to change the text on all the controls. ...
6
by: Michael Groeger | last post by:
Hi, I have an aspx page which generates an excel document and transfers it to the browser as attachment. Normally, once the document is transferred the open save dialog prompts to open or save...
0
by: Jayyde | last post by:
I've tried a few ways to do this now and I can't seem to get it to work. The way it is right now is I've got a LinkButton in a column in a Component Art grid. The column's template is a table with...
2
by: BusyBoy | last post by:
Hi All I am using two aspx pages The first aspx page loads the second page in a popup window. The second aspx page has no html and I change its content type to download a particular file. ...
0
by: de4ever | last post by:
I have used open/save dialog box inside update panel in asp.net.but it is not working because of the code line Response.WriteFile(path);.Can i use any thing else for opening a file through open/save...
1
by: suci | last post by:
hi i have problem in open save as diglox box tahnk
0
by: miztaken | last post by:
Hi Everyone, Is it possible to customize.net's default File Open/Save Dialog Box to display only selected folders in PC according to user's privilege. I want to totally hide the Special Folders...
1
by: ooroboo | last post by:
i wont have made a simple program for my self , with 2 tabs a text document and a web browser , i have had a little look and could not find a way to open a Save As dialog for my Richtextbox is this...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.