473,569 Members | 2,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Download File from C# through Web Method via Ajax call?

40 New Member
I have tried to download the file from the server through the webmethod but it has not work for me. my code as below
Expand|Select|Wrap|Line Numbers
  1. [System.Web.Services.WebMethod()]
  2. public static string GetServerDateTime(string msg)
  3. {
  4.     String result = "Result : " + DateTime.Now.ToString() + " - From Server";
  5.     System.IO.FileInfo file = new System.IO.FileInfo(System.Web.HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["FolderPath"].ToString()) + "\\" + "Default.aspx");
  6.     System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response;
  7.     Response.ClearContent();
  8.     Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
  9.     Response.AddHeader("Content-Length", file.Length.ToString());
  10.     Response.ContentType = "application/octet-stream";
  11.     Response.WriteFile(file.FullName);
  12.     //HttpContext.Current.ApplicationInstance.CompleteRequest();
  13.     Response.Flush();
  14.     Response.End();
  15.     return result;        
  16. }
  17.  
and my ajax call code is as below
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.     function GetDateTime() {
  3.                     var params = "{'msg':'From Client'}";
  4.                     $.ajax
  5.                       ({
  6.                           type: "POST",
  7.                           url: "Default.aspx/GetServerDateTime",
  8.                           data: params,
  9.                           contentType: "application/json;charset=utf-8",
  10.                           dataType: "json",
  11.                           success: function (result) {
  12.                               alert(result.d);
  13.                           },
  14.                           error: function (err) {
  15.  
  16.                           }
  17.                       });
  18.     }
  19. </script>
and i have called this function in button click..

i don't know how to download the file with other methods

Please suggest me if any other methods available or give the correction in the same code.

Thanks to all..
Aug 23 '12 #1
0 2793

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

Similar topics

1
2449
by: Grzegorz Smith | last post by:
Hi everyone. Does anyone know is it possible to check if ajax call was redirect? i mean I connect by Ajax to one URL but I'm redirected to other url. Is there a way to check that my request was redirected? this situation happen when some urls are only accesible when you are logged (i mean you have session) and if you losed session you are...
1
2327
by: Chaprasi | last post by:
Hi, I was wondering how I can achieve to display a 'Please wait loading...' message only if the ajax call is taking more than a second. The message should only appear if the Ajax call is taking more time than a second. In my example I set my ajax timeout for 10 seconds and at my server I sleep for 5 seconds. So in my example
16
3228
by: matt | last post by:
I have used some free code for listing files for download, but I want to send an email to the administrator when the file has been downloaded. I have got some code in here that does it, but it will not print in the username or email amddress of the person doing the download - which I am collecting from a form on the previous page. I can get...
2
5216
by: Zeba | last post by:
Hi guys! I'm new to JS / Ajax; I've been trying to do an Ajax call to my Webservice ( I'm using C# for code-behind). I'm not using any of the libraries available. I am sending my CustID to the webservice and the webservice returns a Dataset that contains various customer details taken from database. I have tested that the Webservice itself...
3
6225
by: Piotrekk | last post by:
Hi My question is: What is the best way to download file giving it maximum timeout ( for example 30 minutes ). After this time the operation should be terminated ( maybe exception ? ). I was thinking about simplest possible solution WebClient class but don't know if it is possible to implement timeout...
3
2058
by: wendallsan | last post by:
Hi All, I've stumped myself writing an app that uses Prototype and a bit of PHP. Here is what I have: I have a custom class named Default_county_init_data that, upon initialization makes several Ajax.Request calls to gather data from the server. What I'm having trouble with is getting the data from the Ajax call back to the custom class...
5
2783
RamananKalirajan
by: RamananKalirajan | last post by:
Hi guys, I am having a problem in Prototypejs AJAX, I am triggering the AJAX call and in the option i am using like the folowing code: new Ajax.Request(connection.url, { method: (connection.method ? connection.method : 'post'), on404: function(request){ alert("File Not Found Exception Occured "); this.connections.waitPop(); ...
9
37282
Claus Mygind
by: Claus Mygind | last post by:
I am having trouble escaping the & in a JSON.stringfy() ajax call. I don't even know if I am stating the problem correctly here. In my app I have linked json2.js from http://www.JSON.org/json2.js 2010-08-25(see attached file) to my app. Then I have created an ajax call in the app to my server_side app in which I invoke the...
5
8804
by: samarinder | last post by:
I am displaying the results by iterating the list in div tag say "results" in my case.For refining of search i am using ajax call. But when i am getting response back from this below snippet document.getElementById("results").innerHTML=xmlHttp.responseText; Its showing me content of JSP in which i am refining my list. I am copying down the...
6
2358
by: agam | last post by:
Here is my ajax call: new Request.HTML({ url: 'users.html', method: 'get', update: 'go', evalScripts: true, /* !! Why isn't this working!? */ onComplete: function(){console.log('ajax complete!')} }).send()
0
7700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7676
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...
0
7974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5513
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...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2114
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
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
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...

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.