ASP表单操作文件(FileSystemObject)

时间:2010-07-12 18:23:39

标签: asp.net javascript forms post action

我的代码是以一种形式发布javascript数组:

<form id="my_form" action="file:///C:/Users/John/Desktop/jquery/savetext.aspx" method="post" onsubmit="return prepare()">
<input type="text" id="file_name" name="file_name" rows="1" cols="20" />
<input type="hidden" name="seatsArray" />
<input type="submit" value="Save" />
</form>

<script type="text/javascript">
function prepare();
{
document.getElementById('seatsArray').value = seatsArray.join();
return true;
}
</script>

任何人都可以帮我解决我需要的savetext.aspx操作文件,因为我对ASP.NET的了解很少(我习惯使用PHP,但这个需要是ASP.NET)。

我想我可以在附近找到一个方面:

<%@ Page Language="C#" %>
<script runat="server">
using System;
using System.IO;

class Test 
{
public static void Main() 
{
    string path = "file:///C:/Users/John/Desktop/jquery/txtfiles/" + request.form("file_name");
    if (!File.Exists(path)) 
    {
        using (StreamWriter sw = File.CreateText(path)) 
        {
            sw.WriteLine(request.form("seatsArray"));
            sw.WriteLine("");
        }   
    }

    using (StreamReader sr = File.OpenText(path)) 
    {
        string s = "";
        while ((s = sr.ReadLine()) != null) 
        {
            Console.WriteLine(s);
        }
    }
}
}
</script>

我是在正确的轨道上吗?

非常感谢!

2 个答案:

答案 0 :(得分:1)

我认为您应该按预期使用表单,只需将数组数据添加到隐藏元素中。

<form id="my_form" action="http://my_site/savetext.aspx" method="post" onsubmit="return prepare()">
  <input type="text" id="file_name" name="file_name" rows="1" cols="20" />
  <input type="hidden" name="seatsArray" />
  <input type="submit" value="Save" />
</form>

<script type="text/javascript">
function prepare();
{
  document.getElementById('seatsArray').value = seatsArray.join();
  return true;
}
</script>

并在服务器端使用request.form("file_name")request.form("seatsArray")

答案 1 :(得分:0)

您需要使用Stream Class。这是使用VB.NET在ASP.NET中编写/创建文本文件的简短代码。

    Dim strStreamW As Stream
    Dim strStreamWriter As StreamWriter
    Try
      Dim ds As New DataSet
      Dim FilePath As String = "C:\nombreArchivo.txt"

      'Open the file, if not exists create it
      strStreamW = File.OpenWrite(FilePath)
      strStreamWriter = New StreamWriter(strStreamW, _
                    System.Text.Encoding.UTF8)

   'Using a conection with the db
   ds = Negocios.TraerDatosArchivo()

   Dim dr As DataRow
   Dim Nombre as String = ""
   Dim Apellido as String = ""
   Dim Email as String = ""

   For Each dr In ds.Tables(0).Rows
     'Get the recordset
      Nombre = CStr(dr("Nombre"))
      Apellido = CStr(dr("Apellido"))
      Email = CStr(dr("Email"))

      'Write the line in the file or "stream"
      strStreamWriter.WriteLine(Nombre & " " & Apellido & " - " & Email)

    Next
    strStreamWriter.Close()

    Catch ex As Exception

      strStreamWriter.Close()

      MsgBox(ex.Message)

  End Try