简体   繁体   English

将 javascript 变量保存到服务器中的文本文件

[英]Save javascript variable to text file in server

I've spent the better half of the past 4 work days trying to find a solution to this issue, but as a newbie coder, I'm yet to find a solution.在过去的 4 个工作日中,我花了大部分时间试图找到解决这个问题的方法,但作为一个新手编码器,我还没有找到解决方案。

Basically I'm interning in this company, and they tasked me with translating a .NET app (C#) from doing its reading and writing of a webpage from a.dll to a.xml.基本上我在这家公司实习,他们要求我翻译 .NET 应用程序(C#),从 a.dll 到 a.Z0F635D0E0F3874FFF8B781C132E6C.7C 的网页读写The page is still opened from the dll, yet all the settings-saving will be done on the.xml, located at./config.xml.该页面仍从 dll 打开,但所有设置保存将在位于./config.xml 的.xml 上完成。 I got it to write the file from the C# end, but when it comes to in-browser, client-side saving, I'm getting some issues.我让它从 C# 端写入文件,但是当涉及到浏览器内、客户端保存时,我遇到了一些问题。 The page looks like this页面看起来像这样

In the previously mentioned DLL, that is hosted using IIS, I can both use C# to program server-side stuff and html through the use of a C# variable from a proprietary method that is used to render the webpage. In the previously mentioned DLL, that is hosted using IIS, I can both use C# to program server-side stuff and html through the use of a C# variable from a proprietary method that is used to render the webpage.

Every checkbox and dropdown (and its content) is dynamically given an HTML ID taking into consideration the divider it lies in and the number of the item, for example "general1", "misc3".每个复选框和下拉菜单(及其内容)都会动态地赋予一个 HTML ID,同时考虑到它所在的分隔符和项目的编号,例如“general1”、“misc3”。 To do that, I used XElement to create the base XML, which is read using C# and plugged into the html, and several javascript scripts to read what checkboxes are checked and what values the dropdowns own. To do that, I used XElement to create the base XML, which is read using C# and plugged into the html, and several javascript scripts to read what checkboxes are checked and what values the dropdowns own.

I'm having issues with the saving part, as this is a webpage meant to be opened on a mobile phone and needs to save the xml file onto the server.我在保存部分遇到问题,因为这是一个要在手机上打开的网页,需要将 xml 文件保存到服务器上。 I've coded it so that, at the end of the page, there's a debug section so I can see the variables and their values, and update the webpage according to the item's contents (basically a way for me to study how getelementbyid and innerhtml work in JS).我已经对其进行了编码,因此在页面末尾有一个调试部分,因此我可以查看变量及其值,并根据项目的内容更新网页(基本上是我研究 getelementbyid 和 innerhtml 的一种方式在 JS 中工作)。 I then process the webpage data, and print out to a然后我处理网页数据,并打印到

what the.xml file would look like, as I stored it on a javascript variable. .xml 文件看起来像什么,因为我将它存储在 javascript 变量中。 My major issue is finding out how to save that javascript variable into a file. 我的主要问题是找出如何将 javascript 变量保存到文件中。 I've read around that I could use php and VB, but I have no clue 1- how to code those and 2- if I can even implement that in this scenario. 我已经阅读过我可以使用 php 和 VB,但我不知道 1-如何编写这些代码以及 2-如果我什至可以在这种情况下实现它。 I've also looked around some javascript ways of doing this, but it often involves the user specifying where to save it LOCALLY, instead of somewhere hardcoded on the server. 我还查看了一些 javascript 执行此操作的方法,但它通常涉及用户指定在本地保存它的位置,而不是在服务器上硬编码的某个位置。 Any help will be incredibly thanked, as I managed to even baffle my higher-ups, so no one here found a solution yet 任何帮助都会非常感谢,因为我什至设法让我的上级感到困惑,所以这里没有人找到解决方案

TL;DR: Got a client-side variable in javascript from an aspx, need to save it locally through javascript to a hardcoded location, maybe using php if you can get me to wrap my head around how it even works TL;DR:从 aspx 获得 javascript 中的客户端变量,需要通过 javascript 在本地将其保存到硬编码位置,也许使用 php 如果你能让我把它包起来的话

Well, first this this idea that some ".dll" is loading the page or data?好吧,首先这个想法是一些“.dll”正在加载页面或数据?

No, you have code behind.不,你有代码。 It has some referneced assembly and library, and the code behin for that page calls that code.它有一些引用的程序集和库,该页面后面的代码调用该代码。 In fact ALL pages work this way.事实上,所有页面都以这种方式工作。

You have plain jane code behind - if the user enters data on that page, then VERY little reason exists as to why the code behind is not then able to freely use + consume the values and data in those controls.您背后有简单的简代码 - 如果用户在该页面上输入数据,那么几乎没有理由说明为什么后面的代码无法自由使用 + 使用这些控件中的值和数据。 VERY strange this issue is not dealt with.很奇怪这个问题没有处理。

I mean, have js pass some value to a method in that code behind and save that data or whatever?我的意思是,让 js 将一些值传递给后面代码中的方法并保存该数据或其他什么? Sure.当然。 (not clear why some class or object structure is not being used here). (不清楚为什么这里没有使用一些 class 或 object 结构)。

I mean, if you want to pass some text to the server, then you can do this:我的意思是,如果你想向服务器传递一些文本,那么你可以这样做:

markup:标记:

        <asp:Button ID="Button2" runat="server" Text="Button" ClientIDMode="Static"/>
        <asp:HiddenField ID="HiddenField1" runat="server" ClientIDMode="Static"/>

        <script>
            function myfuntest() {
                var sometext = "Hello, this is fun"
                $('#HiddenField1').val(sometext)
                $('#button2').click()
            }
        </script>

So in the js code, we shove a value/text into that hidden field.因此,在 js 代码中,我们将一个值/文本推入该隐藏字段。 Click the button.单击按钮。

The code behind will now look like this:后面的代码现在看起来像这样:

protected void Button2_Click(object sender, EventArgs e)
{
Debug.Print("script passed value = " + HiddenField1.Value);
}

So, above now is code behind, and we passed that value.所以,现在上面是代码,我们传递了那个值。

The PROBLEM here is that you attempting to pass xml.这里的问题是您试图通过 xml。 And passing xml is NOT safe when using controls on the form.在表单上使用控件时,通过 xml 是不安全的。 (you have to turn off validation) (您必须关闭验证)

As noted, again: VERY confusing why the value(s) of the controls on that page are NOT picked up by the code behind.如前所述,再次:非常令人困惑,为什么该页面上的控件的值没有被后面的代码拾取。

However, if you wanted to, then yes, you can pass a value to code behind.但是,如果您愿意,那么可以,您可以将值传递给后面的代码。

And you don't even need say a button and hidden field.你甚至不需要说一个按钮和隐藏字段。 Just using a jQuery post (ajax call).只需使用 jQuery 帖子(ajax 调用)。

So, our code will look like this:因此,我们的代码将如下所示:

      <script>

            function test1() {  

                var strXML = '<?xml version="1.0" encoding="utf - 8"?><test>Some text</test></xml>'
        
                $.ajax({
                    type: "POST",
                    url: 'SaveXML.aspx/Test',
                    data: JSON.stringify({sXML : strXML }),
                        contentType: "application/json",  
                        datatype: "json"    
                    });  
                }  

        </script>

So I just shoved some xml into a var.所以我只是把一些 xml 塞进了一个 var。 But my spider sense VERY VERY much thinks a large detail here about that xml strucutre - and having a class server side that de-serlizes that xml is at play here - or should be used.但是我的蜘蛛感觉非常非常多地认为 xml 结构在这里有一个很大的细节 - 并且有一个 class 服务器端去序列化 Z0F635D0E0F3874FFF78B581C132E 应该在这里使用。

but, regardless, the above ajax call and web method will work.但是,无论如何,上述 ajax 调用和 web 方法将起作用。 So on that web page, we can now have a public function called Test, with one paratmers called xXML.因此,在 web 页面上,我们现在可以拥有一个名为 Test 的公共 function,其中一个参数称为 xXML。

The code behind stub can thus look like this:因此,存根背后的代码如下所示:

[WebMethod()]
public static void Test(string sXML)
{
Debug.Print("web method - value data = " + sXML);

string strOutFile;
strOutFile = HttpContext.Current.Server.MapPath("/Content/test.xml");

File.WriteAllText(strOutFile, sXML);
}

So the above would write out the passed xml data from the client side, and the xml data you passed would be written out to a file on the web server under the web server folder called "Content" with a file name of test.xml So the above would write out the passed xml data from the client side, and the xml data you passed would be written out to a file on the web server under the web server folder called "Content" with a file name of test.xml

So it only a wee bit few lines of code.所以它只有很少的几行代码。 But I think overall, there is no doubt additonal details here, and it not quite at a all clear why the data is being gathered up in to some xml, and then sent to the server.但我认为总体而言,这里毫无疑问还有其他细节,而且完全不清楚为什么数据会被收集到一些 xml 中,然后发送到服务器。 If that data is a "group" of data rendered by asp.net controls, then the repeating data can be directly used for a "for each" and seen, used, and grabbed by code behind in place of a whole bunch of client side code that builds up the xml.如果该数据是由 asp.net 控件呈现的数据“组”,那么重复数据可以直接用于“for each”,并被后面的代码查看、使用和抓取,而不是一堆客户端代码建立 xml。

And it also not clear in regards to save the file - you ping pong and alternate the context between saving a file on disk - do you mean client side, or server side?并且关于保存文件也不清楚 - 你乒乓球并在将文件保存在磁盘上之间交替上下文 - 你是指客户端还是服务器端? I had to guess - since you can't save such files client side.我不得不猜测-因为您无法在客户端保存此类文件。

But above does show how to pass a simple var to the server side code behind in that web page, and then write out what you passed to a file on disk.但上面确实显示了如何将一个简单的 var 传递给 web 页面后面的服务器端代码,然后将传递的内容写出到磁盘上的文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM