Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here i have used Asp.net application. I have created XML and downloading using user defined path, But i need to choose a path where it should be downloaded.

Kindly modify this code.

private void create()
    {
        
        XmlDocument docConfig = new XmlDocument();
        XmlNode xmlNode = docConfig.CreateNode(XmlNodeType.XmlDeclaration, "", "");
        XmlElement rootElement = docConfig.CreateElement("HEDDERS");
        docConfig.AppendChild(rootElement);
        
        for (int i = 10; i < 20; i++)
        {
            XmlElement hedder = docConfig.CreateElement("HEDDER");
            docConfig.DocumentElement.PrependChild(hedder);
            docConfig.ChildNodes.Item(0).AppendChild(hedder);
            
            XmlElement installationElement =docConfig.CreateElement("ID");
        XmlText installationIdText = docConfig.CreateTextNode(Convert.ToString(i));
            installationElement.AppendChild(installationIdText);
            hedder.PrependChild(installationElement);
            
            XmlElement environmentElement = docConfig.CreateElement("NAME");
            XmlText environText = docConfig.CreateTextNode("ABC"+i);
            environmentElement.AppendChild(environText);
            
            hedder.PrependChild(environmentElement);
         
        }        
 
        
        docConfig.Save("D:\\Sample.xml");// Here i need to choose a path     
    }


What I have tried:

private void create()
    {
      string downloadFileName = "Template_Xml";
      string currentTime = DateTime.Now.ToString("ddMMyyyyhhmmss");
      fileName = downloadFileName + "_" + currentTime + ".xml";
      System.Web.HttpContext.Current.Response.Clear();
      System.Web.HttpContext.Current.Response.ClearContent();
      System.Web.HttpContext.Current.Response.ClearHeaders();
      System.Web.HttpContext.Current.Response.Buffer = true;
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
                    System.Web.HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
       System.Web.HttpContext.Current.Response.ContentType = "text/xml";
       System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);

        XmlDocument docConfig = new XmlDocument();
        XmlNode xmlNode = docConfig.CreateNode(XmlNodeType.XmlDeclaration, "", "");
        XmlElement rootElement = docConfig.CreateElement("HEDDERS");
        docConfig.AppendChild(rootElement);
        
        for (int i = 10; i < 20; i++)
        {
            XmlElement hedder = docConfig.CreateElement("HEDDER");
            docConfig.DocumentElement.PrependChild(hedder);
            docConfig.ChildNodes.Item(0).AppendChild(hedder);
            
            XmlElement installationElement =docConfig.CreateElement("ID");
        XmlText installationIdText = docConfig.CreateTextNode(Convert.ToString(i));
            installationElement.AppendChild(installationIdText);
            hedder.PrependChild(installationElement);
            
            XmlElement environmentElement = docConfig.CreateElement("NAME");
            XmlText environText = docConfig.CreateTextNode("ABC"+i);
            environmentElement.AppendChild(environText);
            
            hedder.PrependChild(environmentElement);
         
        }        
 
        
        docConfig.Save("D:\\Sample.xml");// Here i need to choose a path 
Posted
Updated 20-Jun-17 23:31pm
v2
Comments
Kornfeld Eliyahu Peter 21-Jun-17 4:11am    
What the significance of the tag 'ASP.NET'?
Does it a web application and you are showing us the server side code?
F-ES Sitecore 21-Jun-17 4:46am    
You can't save files to the client file system and when you send a file to download you can't specify a path there either. This is for security reasons and there is no way around it.

1 solution

You can't do it: the user (and his browser settings) control what happens to the file and you have no control over it at all. That means you can't force the user to open it, run it, or even save it - the browser makes that decision with user input if he has requested it via the browser configuration.

And that is a good thing, if you think about it: what you can do, so can malicious sites. So to prevent them from downloading viruses, ransomware, or objectionable material to the client computer the user is in full control.

Sorry, but you cannot specify it at all unless you application is running on the client machine - and in a web environment, C# code is normally executed at the Server, and Javascript at the client.
 
Share this answer
 

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