views:

41

answers:

2

Hey,

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!

A: 

Hi, 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
eaguilar
I understand that, but my question was about how I could name the txt file according to what the user submitted in the file_name box and how to write in the file the contents of the array. Thanks
IceDragon
+1  A: 

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

Gaby
That looks much more sensible, thanks. I'll update my original question. The action file I made is probably all in the wrong place? Can you tell me what needs changing? Thanks
IceDragon
@IceDragon, try using a path without the `file:///` at the start .. Just a normal local path with backslash instead of slash `string path = "C:\Users\John\Desktop\jquery\txtfiles\" + request.form("file_name");` Tell us if you get an error, and also check to see if the the path variable gets filled the way you want it ..
Gaby
@IceDragon, oh and i think i C# you need to use `Request.Form[ "file_name" ]`. brackets `[]` not parenthesis `()` ..
Gaby
@Gaby, I added your code. Path variable seems to be perfect (text file is created with the specified name). However, the file is empty, so the seatsArray is not getting written to the file. Any idea why? Thanks
IceDragon
the brackets should be use for the seatsArray as well (*in case you missed that*). `Request.Form[ "seatsArray" ]` If you debug the application does the seatsArray contain anything ?
Gaby
Yes I used those brackets for the seatsArray. How would I go about checking if the array contains anything in Visual Web Dev? Thanks
IceDragon
Set a breakpoint and start the debugger.. [Walkthrough: Debugging Web Pages in Visual Web Developer ](http://msdn.microsoft.com/en-us/library/z9e7w6cs%28VS.80%29.aspx#CodeSpippet5)
Gaby
Do I set it on "seatsArray.push(desc + e.posX, e.posY);" at the end of the document? Thanks
IceDragon
no set the breakpoint at the ` sw.WriteLine(request.form["seatsArray"]);` line. This way you can inspect the return from the `request.form` right when it occurs.
Gaby
Some BS about the breakpoint not being hit because of symbols. Gonna try to fix this and will let you know. Thanks
IceDragon