Click here to Skip to main content
12,349,911 members (23,407 online)
Rate this:
 
Please Sign up or sign in to vote.
I call MyHandler from the client through ajax using mootoools
In Myhandler, I send some data back, that should be exported to excel in client.
The code in ProcessRequest in the MyHandler is following:
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.ContentType = "application/vnd.xls";

Response.Write("FirstName");
Response.Write(Environment.Newline);
Response.Write("Igor")"
Response.End();

When the response is returned to the client, no dialog is shown for asking to save the file. The server returns just xml. How to do that when the response is returned to client, the dialog will be shown and ask to save the attachment?

Thanks
Posted 5-Oct-10 7:27am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

I don't know about doing this via ajax, but you can do this easily with a hidden iframe on your page:

page1.aspx:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function test() {
            document.getElementById("ifr").src = "page2.aspx";
        }
    </script>
</head>
<body >
    <form id="form1" runat="server">
    <asp:Button ID="doit" runat="server" OnClientClick="test(); return false;" />
    <iframe id="ifr" style="display:none" />
    </form>
</body>
</html>


Page2.aspx:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Page2.aspx.vb" Inherits="Page2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
</body>
</html>




page2.aspx.vb:

Partial Class Page2
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.Clear()
        Response.AddHeader("content-disposition", "attachment;filename=FileName.xls")
        Response.ContentType = "application/vnd.xls"
        Response.Write("FirstName")
        Response.Write(Environment.NewLine)
        Response.Write("Igor")
        Response.End()
    End Sub
End Class
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160621.1 | Last Updated 5 Oct 2010
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100