2010-07-12 34 views
1

我有我的代碼張貼在表單中的JavaScript數組:ASP形式的操作文件(FileSystemObject的)

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

我在正確的軌道上?

非常感謝!

+0

所以,你想要有人爲你做你的工作嗎?你必須更好地問。順便說一句,.asp不是ASP.NET。 – 2010-07-12 18:31:13

+0

我只需要一些代碼來完成這個該死的事情。否則,我將不得不花費數週時間來學習如何預先做好。哦,我沒有意識到有什麼不同。這是ASP.NET我需要具體。 – IceDragon 2010-07-12 18:35:38

+0

我的意思不是粗魯,但爲什麼你需要用你不熟練的語言來做某些事情,而不學習語言? – 2010-07-13 14:33:30

回答

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

+0

這看起來更合理,謝謝。我會更新我原來的問題。我所做的動作文件可能都在錯誤的地方?你能告訴我什麼需要改變?謝謝 – IceDragon 2010-07-14 02:52:49

+0

@IceDragon,嘗試在開始處使用不帶'file:///'的路徑..只是一個帶反斜槓而不是斜槓的普通本地路徑 ''''''' txtfiles \「+ request.form(」file_name「);'告訴我們,如果你得到一個錯誤,並檢查路徑變量是否得到你想要的方式填充.. – 2010-07-14 08:50:21

+0

@IceDragon,哦,我想我C#你需要使用'Request.Form [「file_name」]'。括號'[]'不括號'()'.. – 2010-07-14 08:54:05

0

您需要使用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 
+0

我明白,但我的問題是關於如何根據用戶在file_name框中提交的內容命名txt文件以及如何在文件數組的內容。謝謝 – IceDragon 2010-07-12 21:20:46