473,396 Members | 1,792 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,396 software developers and data experts.

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

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 2759

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

Similar topics

1
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...
1
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...
16
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...
2
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...
3
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...
3
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...
5
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:...
9
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...
5
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 ...
6
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:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.