简体   繁体   中英

ASP form action file (FileSystemObject)

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!

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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