473,756 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

31 New Member
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="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
  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.Http Context.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_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.
Jul 2 '10 #1
0 2830

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

Similar topics

2
1760
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
3197
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 been unable to determine a method of forcing the dialog box to open in the centre of the screen (or any other position on the screen). How can l control the position on the screen in which the dialog box opens ? Any help would be greatly...
2
2351
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. Mainly cause my boss wanted the Open File Dialog's OK button to read 'Import', for all intensive purposes. Open\Save dialogs where constructed purely from the Win32 API as you cannot inherit these classes from the framework. Problem I am recounting...
6
2698
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 the excel document. When I click save I can save the excel document or when I click open it directly opens in excel. This works fine for all but one machine. On one machine however the dialog prompts to open or save the excel document (e.g....
0
926
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 mulitple controls in it (one being this link button). I've been reading newsgroups and the farthest I've gotten is adding the Response.AddHeader stuff to an ItemCommand event for this guy, but what that does at the moment is give me the...
2
2501
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. When I get the 'Open Save Cancel' dialog box in case I click on Cancel
0
2509
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 dialog box.Waiting for replies... My code for opening the file: Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); Response.AppendHeader("content-disposition", ...
1
1332
by: suci | last post by:
hi i have problem in open save as diglox box tahnk
0
1141
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 i.e. Desktop, Program Files and Windows from the hierarchy. For other folders list i can always use database per user. Using file permission i can restrict the access but my whole problem lies in hiding the folders ? I am using VS.net 2005 (C#)...
1
1257
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 possible or how should it be done? ( i wont the Save as dialog to save the richtextbox.text as a file with an option to change the file name) thanks for your help/ time
0
9255
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10014
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9819
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8688
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7226
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5289
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3780
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3326
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2647
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.