Just nu i M3-nätverket
Gå till innehåll

Gör en txt fil av ett form?


Rickard M

Rekommendera Poster

Jag undrar om någon kan hjälpa mig med att skapa en txt fil från "File" som användaren skriver in i formet "File". Alltså jag vill att filen skall döpas efter det som användaren skriver in.Har jag inte förklarat tillräckligt återkommer jag.

--------------------------------------------------

<%

 

Response.Cachecontrol="Public"

 

Namn = Request("Namn")

File = Request("File")

ID_Category = Request("Dropmenu")

Description=Request("Description")

 

 

accessdb="/cgi-bin/Scripts.mdb"

Set Connection = Server.CreateObject("ADODB.Connection")

Connection.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & server.mappath(accessdb)

 

 

If Namn = "" Then

Response.Redirect("error.asp")

Else

 

If File = "" Then

Response.Redirect("error.asp")

Else

 

If Description = "" Then

Response.Redirect("error.asp")

Else

 

If ID_Category = "Select" Then

Response.Redirect("error.asp")

Else

 

SQLStmt = "INSERT INTO tblScript"

SQLStmt = SQLStmt & "(Namn, File, ID_Category, Description)"

SQLStmt = SQLStmt & "VALUES('" & (Request.Form("Namn")) & "', '" & (Request.Form("File")) & "', '" & (Request.Form("Dropmenu")) & "', '" &(Request.Form("Description")) & "')"

Connection.Execute(SQLStmt)

 

 

 

 

End if

End if

End if

End if

%>

Länk till kommentar
Dela på andra webbplatser

Kolla på följande artikel, kanske kan ge dig något....

 

___________________________________________

 

Ahh.. That feeling that comes from writing another article. You have to experience to believe it. Anyways, this tutorial is for the beginner to intermediate developer who wants to write to a text file. This purpose can be anywhere from storing text in a nice global format, to writing batch files to execute on the server. Now, belive it or not, I will NOT be linking this to a real world example. Why? Because the only one's I can think of involve ideas I plan to market. ;-) So now we are left with this.

 

Now, I will show you the basic commands for reading and writing, with a little bit of code snippits to keep everyone happy. It is very very simple. First, writing:

 

set FSO = Server.CreateObject("scripting.FileSystemObject")

 

This line creates an object that is used for File Access

 

set myFile = fso.CreateTextFile("C:\test.txt", true)

 

This creates a blank text file object for us to use.The CreateTextFile object creates a text file, based on what we specified. The first option specifies the location of the text file, while the second one states whether or not to create the file if one doesn't exist. Very simple methods.

 

Next, we do something simple

 

myFile.WriteLine("my text here")

myFile.WriteLine("more text here")

 

.WriteLine writes the text to the text file. What a novel idea! =D

 

myFile.Close

 

This closes the file, and makes it accessible to all (for the good old days of File Sharing). It also frees up memory.

 

Now for Reading:

 

First, we create an object, but not after defining some constants

 

Const ForReading = 1, ForWriting = 2, ForAppending = 8

 

This just allows us the different methods of working with text, when we have an open text file.

 

set fso = server.CreateObject("Scripting.FileSystemObject")

 

Here we create our object again.

 

set f = fso.GetFile("C:\test1.txt")

 

Here we state that we will be manipulating the file C:\test1.txt . Moderately simple. Now we need to open it as a stream. Get ready, here comes the hard stuff ;-)

 

set ts = f.OpenAsTextStream(ForReading, -2)

 

This creates a text stream with our file. It is like a continous flow of information. Now we jsut read a line

 

TextStreamTest = ts.ReadLine

 

This code reads a line of information, and puts it into TextStreamTest. Pretty simple, isnt it?! I hope so. Anyway, I've discussed the code, and I shall let you go.

 

One last note. Let's say you wanted to read all the contents out a text file. Then you would do this

 

Do While not ts.AtEndOfStream

myText = myText & ts.ReadLine & vbCrLf

Loop

 

This tells it do a loop while there is still text, then read a line and add a line break. Very easy ;-)

Länk till kommentar
Dela på andra webbplatser

Arkiverat

Det här ämnet är nu arkiverat och är stängt för ytterligare svar.

×
×
  • Skapa nytt...