Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi,
 
Can someone please tell me how to save an xml file from a site Url to local system by prompting through Save Dialog box.
 
I have written the following code rather writing the whole content on browser window itself rather than prompting through dialog box. Please tell me where I doing wrong.
 
string Filename = "http://localhost/SiteName/FileName.xml";
        XmlDocument doc = new XmlDocument();
        doc.Load(Filename);
        string strXML = GetXMLString(doc);//Converts XMLDocument to string

        Response.Clear();
        Response.ClearHeaders();
        Response.ClearContent();
        Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        Response.ContentType = "application/xml"; //or text/xml

        Response.AddHeader("content-disposition", String.Format("attachment;filename=" + Filename));
        Response.WriteFile(strXML);     
        Response.Flush();
        Response.Close();
        Response.End();
Thanks.
Posted 24-Jul-12 13:10pm
InfySam187
Edited 24-Jul-12 13:42pm
Wes Aday61.7K
v2
Comments
Sergey Alexandrovich Kryukov at 24-Jul-12 20:07pm
   
You are not trying to write a file, so how can it be wrong? :-) --SA
lukeer at 25-Jul-12 2:47am
   
Looks like you are working on some web-based stuff. The user's browser should then be able to save a file. It does so by showing the user a dialog box he is familiar with. You would only have to provide a link.

1 solution

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

Solution 1

Look at the following: http://msdn.microsoft.com/en-us/library/8bh11f1k.aspx[^]
 
Here is the basic code for an open dialog for WinForms (WPF would be slightly different):
 
var save = new SaveFileDialog();
var open = new OpenFileDialog();
var result = open.ShowDialog();
if (result == DialogResult.OK)
    MessageBox.Show("Filename selected to open is " + open.FileName);
 

See http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.aspx[^] for more information on OpenFileDialog, and http://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog.aspx[^] for SaveDialog
  Permalink  
v2
Comments
InfySam at 24-Jul-12 21:16pm
   
Pardon me if I am not clear regarding my requirement. I want to display a Save/Open/Cancel dialog box inorder to save the xml file(This xml file is from a site) on local system.
Clifford Nelson at 24-Jul-12 22:56pm
   
Must be that you want to save a file on a remote system. Am I correct. Otherwise it is just using the Open/Save dialogs. If you do want to save/open to a remote system, you have to have installed program on the remote system because Windows does not like to allow remote access to a oomputer for obvious reasons. One way to do this is work thru the PowerShell remote features.
InfySam at 25-Jul-12 8:47am
   
Hi all, Thanks for your replies. My apologies because this seemed I did not properly state my requirement (my bad :(). All that I want is on click of a button I want a save/open dialogbox to appear. Can someone please help me with this?
Clifford Nelson at 25-Jul-12 12:46pm
   
I have added to my response. Hope that helps.
InfySam at 25-Jul-12 13:35pm
   
Hi, Adding above code in my button click event gives the following error, Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application. I am still unable to resolve this problem. But I prefer open/Save download dialog box to the SaveFileDialog(), OpenFileDialog() and I think this can be achieved by using Content-Disposition. However using the following code is displaying content in the browser rather than popping up Open/Save/Cancel download dialog box. string Filename = "http://localhost/SiteName/FileName.xml"; XmlDocument doc = new XmlDocument(); doc.Load(Filename); string strXML = GetXMLString(doc); HttpResponse response = HttpContext.Current.Response; response.Clear(); response.ClearContent(); response.ClearHeaders(); response.Buffer = true; response.AddHeader("ContentType", "application/octet-stream"); response.AddHeader("Content-Disposition", "attachment; filename=\"FileName.xml\";"); response.Write(strXML); response.End();
Clifford Nelson at 25-Jul-12 13:57pm
   
You must not be running in Desktop. Are you running ASP.NET or Silverlight.. I have seen issues with STA (single thread apartment) but not this. Look at http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/c409b6b6-acef-470a-b4b3-4b48e210dfc6/. I suspect you will have to do some custom dialogs. I am not an ASP.NET developer. THis may also help: http://forums.asp.net/p/978886/1247167.aspx

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Guruprasad.K.Basavaraju 460
1 Sergey Alexandrovich Kryukov 336
2 Shai Vashdi 294
3 OriginalGriff 280
4 Abhinav S 160
0 Sergey Alexandrovich Kryukov 9,185
1 OriginalGriff 5,315
2 Peter Leow 4,040
3 Maciej Los 3,535
4 Abhinav S 3,308


Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 25 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid