【问题标题】:How to save text values from web page locally and is it possible to have web page front end, C# back end?如何在本地保存网页中的文本值,是否可以有网页前端、C# 后端?
【发布时间】:2014-11-03 22:50:27
【问题描述】:

我正在创建一个网页,其中包含许多文本输入框,如下例所示。

<input type="text" id="textID" name="textName" placeholder="Enter your text here">

我将在页面底部有一个按钮,我想从每个文本框中获取所有值,如果可能的话最好序列化为 XML 格式,然后在本地保存。如果无法进行序列化,则可以保存到 txt/doc/etc。我正在使用 javascript 来完成这项工作。

我已经知道如何在单击时让我的按钮在本地保存我选择的文档,但是我必须使用 Web url 来提取,这只会保存页面源代码。那对我没有好处。我不知道用什么来代替下面的 save.href。 save.href 强制使用我不想要的 URL。

<HTML>
//web page stuff

//Many of these inputs like this one below
<input type="text" id="textID" name="textName" placeholder="Enter your text here">

<script language="javascript" type="text/javascript">

function SaveData() 
{
  var fileURL = "http://localhost:51088/index.html";
  var fileName = "test.txt";


  if (!window.ActiveXObject) 
  {
    var save = document.createElement('a');
    save.href = fileURL;
    save.target = '_blank';
    save.download = fileName || 'unknown';

    var event = document.createEvent('Event');
    event.initEvent('click', true, true);
    save.dispatchEvent(event);
    (window.URL || window.webkitURL).revokeObjectURL(save.href);
  }

    // for IE
  else if (!!window.ActiveXObject && document.execCommand) 
  {
    var _window = window.open(fileURL, '_blank');
    _window.document.close();
    _window.document.execCommand('SaveAs', true, fileName || fileURL)
    _window.close();
 }
}
</script>
</HTML>

上面的代码正在生成一个文本文件,该文件可以本地下载到我的盒子,但它只是我提供的 URL 的页面源代码。如何从文本框中获取所有值并将这些数据保存在本地?

我知道我可以执行下面这个示例来获取文本框的值,但是我怎样才能获取该值并保存它?

var textBoxValue = document.getElementById("textID").value;

我最终想要采取的另一条路径是以某种方式编写可以直接绑定到网页上的文本框的 C# 业务层代码。如果不绑定,至少将值传递给 C# 代码。如果我能做到这一点,那么我可以在 C# 中处理格式化、保存等。是否可以有一个 HTML 网页,以 JavaScript 作为 UI,C# 作为后端?

【问题讨论】:

    标签: javascript c# jquery html web


    【解决方案1】:

    是否可以有一个 HTML 网页,以 JavaScript 作为 UI,C# 作为后端?

    是的,至少可以使用来自 Visual Studio 的网站项目甚至是 Web 应用程序项目。

    此外,How to write data to textfile using JavaScript 的答案可能是您正在寻找解决将文本框值保存到文本文件的问题。

    【讨论】:

    • 你有链接指向我一些关于如何使用 HTML 作为 UI 和 C# 作为后端的资源吗?另外,我查看了编写文本文件的链接,但没有成功。
    • 这是您要找的吗?它非常简短,但解释了要点和方法。 msdn.microsoft.com/en-us/library/ms243156(v=vs.100)
    【解决方案2】:

    我已经设法弄清楚如何从每个文本框中获取值并将它们保存到文本文件中。下面的代码示例就是这样做的。现在研究如何将其转换为 XML 格式。

    var userInput = document.getElementById("userInputId").value;
    
    var fileURL = 'data:application/notepad;charset=utf-8,' + encodeURIComponent(userInput);
    var fileName = "test.txt";
    
    
    if (!window.ActiveXObject) {
      var save = document.createElement('a');
      save.href = fileURL;
    
      save.target = '_blank';
      save.download = fileName || 'unknown';
    
      var event = document.createEvent('Event');
      event.initEvent('click', true, true);
      save.dispatchEvent(event);
      (window.URL || window.webkitURL).revokeObjectURL(save.href);
    }
    
    // for IE
    else if (!!window.ActiveXObject && document.execCommand) {
      var _window = window.open(fileURL, '_blank');
      _window.document.close();
      _window.document.execCommand('SaveAs', true, fileName || fileURL)
      _window.close();
    }
    

    【讨论】:

      猜你喜欢
      • 2016-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多