Forums

ASP

This topic is locked

open search result in text file

Posted 06 Jun 2007 08:41:14
1
has voted
06 Jun 2007 08:41:14 neha shrivastava posted:
hello
Can any one please help me in the following.....
I making a database with Access and ASP. I have created a search option where I have a text box and a submit button. the result to this search is displayed in the same page.
all i want is a option where user can choose between printing the results in the same page or in a text file....
the result is in tabular form and i want the format to remain if not table it self (in the asp page the results is presented in html tabular format with borders in the table so i want same thing to occur in text file to if it is possible)

please help
thanks
neha

Replies

Replied 08 Jun 2007 22:35:15
08 Jun 2007 22:35:15 dave blohm replied:
here's how I would do it...this was written quickly so it's not pretty but it works. As text files can be rendered in a browser, in this example the user must right-click on the link and choose 'save as'.

you can view it in action at www.daveblohm.com/dmxzone/fsocreatetextfile/index.asp

<pre id=code><font face=courier size=2 id=code>&lt;%
if request.form &lt;&gt; "" then
' ::::: THE USER HAS SUBMITTED THE FORM
if request.form("display" = "1" then
' ::::: THE USER HAS CHOSEN TO DISPLAY THE RESULTS AS HTML
response.write "this: " & request.form("txt_this" & "&lt;br /&gt;"
response.write "that: " & request.form("txt_that"
else
' ::::: THE USER HAS CHOSEN TO EXPORT THE RESULTS AS A TEXT FILE
' :::::
' ::::: ON THE NEXT LINE I'D GET THE USER_ID FROM THE SESSION VARIABLE
' ::::: OR WHATEVER YOU ARE USING TO KEEP TRACK OF WHO YOUR USER
' ::::: IS
usr_id = "jsmith"
' ::::: GET THE PHYSICAL ROOT OF THE WEBSITE (C:\SOME_PATH\SOME_DIRECTORY)
p_root = server.MapPath("/"
' ::::: TELL THE APPLICATION WHERE YOU ARE GOING TO STORE YOUR TEXT FILES
path_from_root = "\dmxzone\fsocreatetextfile\my_files_collection\"
' ::::: BUILD THE FULL PHYSICAL PATH
p_path = p_root & path_from_root
' ::::: NEXT, GENERATE THE FILE NAME
' ::::: CREATE A UNIQUE FILE NAME BASED ON THE USER_ID AND NOW()
' ::::: AND REPLACE ALL THE SPECIAL CHARACTERS THAT CAN BE IN A
' ::::: DATE/TIME TYPE FIELD. APPEND THE FILE EXTENSION TO THE END
' ::::: NOTE: THIS METHOD CAN ALSO BE USED TO GENERATE STATIC "REAL
' ::::: FILE" HTML PAGES FROM A DYNAMIC SOURCE...JUST CHANGE THE
' ::::: EXTENSION TO .HTML
file_name = usr_id & replace(replace(replace(now(),"/",""," ","",":","" & ".txt"
' ::::: CREATE A SCRIPTING.FILESYSTEMOBJECT OBJECT
Set fso = CreateObject("Scripting.FileSystemObject"
' ::::: TELL THE OBJECT WHERE TO PUT THE FILE AND WHAT TO CALL IT
' ::::: AND CREATE IT
Set fo = fso.CreateTextFile(p_path & file_name , True)
' ::::: GENTERATE THE CONTENT OF THE TEXT FILE
fo.WriteLine "These are the results:"
fo.writeline
fo.WriteLine "This: " & request.form("txt_this"
fo.WriteLine "That: " & request.form("txt_that"
' ::::: CLOSE THE OBJECT
fo.Close
' ::::: WRITE OUT THE LINK TO THE FILE
response.write "&lt;a href=/" & path_from_root & file_name & "&gt;file here&lt;/a&gt;&lt;br &gt;"
end if
else
' ::::: THE USER HAS NOT SUBMITTED THE FORM, SO SHOW THE FORM %&gt;
&lt;form id="frm_myform" name="frm_myform" method="post" action=""&gt;
This field
&lt;input name="txt_this" type="text" id="txt_this" /&gt;
&lt;br /&gt;
That field
&lt;input name="txt_that" type="text" id="txt_that" /&gt;
&lt;br /&gt;
&lt;input name="display" type="radio" value="1" checked="checked" /&gt;
Display as HTML
&lt;input name="display" type="radio" value="2" /&gt;
Export as text file
&lt;br /&gt;
&lt;br /&gt;
&lt;input type="submit" name="Submit" value="Submit" /&gt;
&lt;/form&gt;
&lt;% end if %&gt;</font id=code></pre id=code>


Also...by using server.contenttype you can export as word, excel, et cetera

- Doc

Progress is made by the discontent.

Edited by - daveblohm on 08 Jun 2007 22:36:46

Edited by - daveblohm on 08 Jun 2007 22:38:32

Edited by - daveblohm on 11 Jun 2007 20:35:20
Replied 11 Jun 2007 12:01:09
11 Jun 2007 12:01:09 neha shrivastava replied:
hey dave

thanks a million this code was a life saver.......
i was working on something similar from a long time but it was all waste with ur guidence i made necessary changes required & could do what i wanted my code to do.
thanks again it worked as u told.....
take care
neha
Replied 20 Jun 2007 05:46:58
20 Jun 2007 05:46:58 faziey una replied:
hello dave...
i also want to thanks for this code..
actually i also have a similar problem with neha and still working on it...
but the different is i'm using a JSP...
so anyone here can help me??
by the way..i'm newbie here and i don't no nothing about JSP.
Plese anyone help me...

Reply to this topic