Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
5.00/5 (1 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

1 solution

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

page1.aspx:

XML
<%@ 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:

XML
<%@ 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:

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
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900