0

I have tried many articles on net for downloading a text file using JQuery, Ajax and C#. Most of them says you cannot download file using ajax.

Here is my JQuery-Ajax code

 $(document).on("click", "#imgDownload", function (event) { $.ajax({ url: "/members/DownloadSelectedFile?SelectedUserName=" + $("#AllowedFriends").find(":selected").text() + "&SelectedFileName=" + $(this).siblings("span").eq(0).text(), success: function () { alert("Khushi"); } }); }); 

Here is my C# code

public void DownloadSelectedFile(string SelectedUserName, string SelectedFileName) { Response.Clear(); Response.ContentType = "application/octet-stream"; Response.AppendHeader("Content-Disposition", "Filename=" + SelectedFileName + ".txt"); Response.TransmitFile(Server.MapPath("~/Users/" + SelectedUserName + "/" + (string)Session["LoggedInUserName"] + "/" + SelectedFileName + ".txt")); Response.End(); } 

So, what are the changes that I need to make to be able to download a text file.

1

1 Answer 1

4

Add an IFrame to your document, either runat=server or not.... The following example requires you keep it client

<iframe id="myDownloaderFrame" style="display:none;" ></iframe> $(document).on("click", "#imgDownload", function (event) { $("#myDownloaderFrame").attr("src","/members/DownloadSelectedFile?SelectedUserName=" + $("#AllowedFriends").find(":selected").text() + "&SelectedFileName=" + $(this).siblings("span").eq(0).text()); }); 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks fahadash, This solves my problem. But can you explain me what is iFrame?
IFrame allows you to have web-page inside a web-page as an control. Much like PIP feature on your latest TV.... This is going to be the only way you can do frames in HTML5, in earlier HTML versions you could use <frameset> too.
I remember using <frameset>. I used it to divide the page in many parts while I was learning HTML. Thanks for explanation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.