C# save xml file locally only

2019-03-06 12:19发布

I've been searching everywhere to try to find the answer to this, but haven't found an answer.

My ASP.NET web form saves user input to an xml file first to the server and then downloads the server file to the user locally,, but I want the file to be saved on the user's machine only, NOT the server. I've figured out how to download the xml to the user's machine from the server, but I don't want to do this because connectivity is an issue. I'd like the user to access the web form, input the values and then have the program save the file JUST locally on the user's machine. Here's my code so far to save the file...

DateTime todaysDate = DateTime.Today;
                    string fileName = Regex.Replace(userName, @"'", "") + "_" + todaysDate.ToString("yyyy_MM_dd") + ".xml";


                    new XDocument(
                         new XElement("NewUser",
                         new XElement("userName", Regex.Replace(userName, @"'", "")),
                         new XElement("Role", Regex.Replace(Role, @"'", "")),
                         new XElement("Title", Regex.Replace(Title, @"'", "")),
                         new XElement("Location", Regex.Replace(Location, @"'", "")),
                         new XElement("DocumentSecurity", Regex.Replace(DocumentSecurity, @"'", ""))
                                )
                            )
                    .Save("C:\\" + fileName);
                    String FilePath = "C:\\" + fileName;
                    System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
                    response.ClearContent();
                    response.Clear();
                    response.ContentType = "text/plain";
                    response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ";");
                    response.TransmitFile(FilePath);
                    response.Flush();
                    response.End();

2条回答
戒情不戒烟
2楼-- · 2019-03-06 12:57

"I'd like the user to access the web form, input the values and then have the program save the file JUST locally on the user's machine. Here's my code so far to save the file..." What you asked for used to be possible. And it was so abuseable, so prone to error it was removed without replacement. And nope, nobody that lived in those times misses them.

The final Authority on what it does and does not do is the Browser itself. You can ask it nicely to save a cookie, but it might just say "no" and there is not a thing you can do about it. And that is only because we know where the alternative leads. We learned it the hard way.

Something like your goal is possible, but not without one concession or another:

  • You could have the client down/upload a xml file with entered data to your Webformular. The usual Down- and Upload Dialogs apply and user activity apply. Some average applications of "E-Government" use this approach, if saving on the Server is somehow not possible. With a Memory stream and the XML classes, no actuall filesystem access is nessesary on the Webserver side.

  • Alternatively you could have a Application on the user side. This can be a normal Desktop Application, a Appstore App, ClickOnce Deployment* even a Java Programm*. Main thing is that you have a Client Programm that was allowed onto the Computer by the user and thus has at least the rights to read/write the User Profile folder.

  • Of course you also do not need to store the XML onto the disk directly. If you got a Database, you migh as well saved it there. XML is in the end just a text format. A markup langauge like HTML. So it fits in just about any VARCHAR column. And there might even be a special XML type in your DBMS of choice.

*I am not 100% sure those can actually write randomly. It could be one of the tradeoffs for the ease of Deployment.

查看更多
成全新的幸福
3楼-- · 2019-03-06 12:59

I think there is no way to store file on client side (browser) to disc because of security. The only way you can do this is to send your XML to server, save it in temporal file (or memory stream) and after what provide link to user to download result.

查看更多
登录 后发表回答