ASP 表单操作文件 (FileSystemObject)

发布于 2024-09-09 06:59:02 字数 1346 浏览 2 评论 0原文

我已经得到了以表单形式发布 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>

我走在正确的轨道上吗?

非常感谢!

I've got my code to post a javascript array in a form:

<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>

Can anyone help me out with the savetext.aspx action file that I need, as my knowledge of ASP.NET is minimal (I'm used to PHP, but this one needs to be ASP.NET).

I guess I can have a crack at getting somewhere near:

<%@ 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>

Am I on the right track?

Many Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹊巢 2024-09-16 06:59:02

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

<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("file_name") 。表单(“seatsArray”)

I think you should just use the form as intended, and just add the array data to a hidden element..

<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>

and on the server side use request.form("file_name") and request.form("seatsArray")

那支青花 2024-09-16 06:59:02

您需要使用 Stream 类。这是使用 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

you need use the Stream Class. This is a short code for write / create text files in ASP.NET using VB.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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文