Download a file via Webservice?

Last post 11-04-2009 2:24 PM by codeasp. 1 replies.

Sort Posts:

  • Download a file via Webservice?

    11-04-2009, 10:46 AM

    Hi, I have a webservice method which is going to download a file (pop up window with Open or Save the file). I notice webservice might not accept HttpContext.Current.Response?? I have below method which doesn't work for me and prompt error "System.Xml.XmlException: Data at the root level is invalid" .. .but if i created another method just return a test string value, pass to my ASPX page and print it out, it works ... :( It just doesn't print the line from the webmethod. Any idea or workaround this?


    [WebMethod]  
        public void DownloadFile(string fileName)
        {
            string filepath = fileName;

            FileInfo file = new FileInfo(filepath);

            if (file.Exists)
            {
     
                HttpContext.Current.Response.ClearContent();

                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);

                HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());

                HttpContext.Current.Response.ContentType = ReturnExtension(file.Extension.ToLower());

                HttpContext.Current.Response.TransmitFile(file.FullName);

                HttpContext.Current.Response.End();
            }
        }


  • Re: Download a file via Webservice?

    11-04-2009, 2:24 PM
    Answer
    • Star
      12,579 point Star
    • codeasp
    • Member since 06-16-2004, 5:19 PM
    • Posts 2,201

    Does the following link help?

    http://forums.asp.net/t/1175307.aspx

    http://www.c-sharpcorner.com/UploadFile/gnsrinivas1511/Webservice05112009034709AM/Webservice.aspx

    http://forums.asp.net/p/1119431/2601637.aspx#2601637

Page 1 of 1 (2 items)