locked
excel file not download cause of Webmethod [System.Web.Services.WebMethod] public static void GetServerDate(string xmldata) { RRS feed

  • Question

  • User1610357552 posted
    
    
    [System.Web.Services.WebMethod]
            public static void GetServerDate(string xmldata)
            {  
    HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
    
    HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=MyFile.xlsx");
    
    HttpContext.Current.Response.TransmitFile(HttpContext.Current.Server.MapPath("~/report/Book1.xlsx"));
    
    HttpContext.Current.Response.End();

    }



    Friday, August 22, 2014 5:45 AM

Answers

  • User1918509225 posted

    Hi saritapatil.888,

    For your problem , I wrote a demo test it.

    It worked very well in  my computer.

    I create a button in the page, when I click the button  it will  call the GetServerDate () method.

    Below is my code:

     <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        </div>
     </form>
     protected void Button1_Click(object sender, EventArgs e)
            {
                GetServerDate();
            }
    
            [WebMethod]
            public static void GetServerDate()
            {
                HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=test.xlsx");
                HttpContext.Current.Response.TransmitFile(HttpContext.Current.Server.MapPath("~/XmlDemo/tss.xlsx"));
                HttpContext.Current.Response.End();
    
            }

    I suggest that you can set a breakpoint on your GetServerDate() method, debug it and check if it can be hit and see if it has any error message.

    if does ,please post here.

    Best Regards,

    Kevin Shen.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 24, 2014 9:28 PM