We help IT Professionals succeed at work.

SERVER Side File Opening Problem

witgrefe
witgrefe asked
on
Medium Priority
278 Views
Last Modified: 2008-02-26
I have put the following code in an asp file to try and create a file on the server using JavaScript.  all I get is the code that is between the <SERVER> and </SERVER> tags written to the screen.  What must I do to write files to the server.  (I got the example from SAMS Pure JavaScript).
<BODY bgcolor=#d3d3d3 onload ="initialise()">
<SERVER>
     var myFile = new File("/test.txt");
     myFile.open("w");
     myFile.writeln("TEST");
     myFile.close();
</SERVER>
Comment
Watch Question

Nitin SontakkeDeveloper
CERTIFIED EXPERT

Commented:
I am sorry, but i never such a code before.

It will be appropriate if you could let us know the environment in which you intend to run your code. Is it IIS/ASP (where VBScript will be more appropriate to use) or some other web-server where javaScript is a native server side programming language.

Commented:
If you're writtng in ASP, you don't need Javascript server side then. Make file in ASP code (between <% ... %>)

And make sure you have write permissions in place you're trying to put new file.

Author

Commented:
What I am trying to do is get a second list box to be populated depending on what is selected in the first list box.  The elements for both list boxes are got from an SQL database.  I have tried using VBScript/ASP but I can only get the change recognised after the asp page has been loaded a second time.  Here is that portion of my asp page:
<TR><TD><SELECT ID="imgCountrySelect" NAME="ics" onChange="update()">    
               
                    <%    
                         dim imagesStr, ctyStr, mytextstream, myfilesystem, strSelected
                         
                         set myfilesystem=server.createobject("scripting.filesystemobject")    
                         set mytextstream=myfilesystem.opentextfile("c:\temp.txt")
                         ctyStr=mytextstream.readLine    
                         if ctyStr="" then ctyStr="none"    
                         mytextstream.Close    
                         set mytextstream = Nothing                                        
                         rs.Open "SELECT * FROM [Country List]"
                         Do While Not rs.EOF

                         If rs.Fields("CountryName").Value =ctyStr then
                              strSelected = "selected"
                              else strSelected = ""
                         end if
                    %>    
                    <OPTION VALUE="<%=rs.Fields("CountryName").Value%>"<%=strSelected%>>
                         <%=rs.Fields("countryName").Value%>          
                    <%
                         rs.MoveNext
                         Loop    
                         rs.Close                              
                    %>
               </SELECT></TD><TD>              

               <SELECT ID="selectForEditing" NAME="ifn">    
     
                    <%                    
                         images="SELECT * FROM [Images List] where imageCountryName like '" + ctyStr + "'"          
                         rs.Open images
                         Do While Not rs.EOF
                    %>    
                         <OPTION VALUE="<%=rs.Fields("imageFileName").Value%>">
                                   <%=rs.Fields("imageFileName").Value%>          
                    <%
                         rs.MoveNext
                         Loop    
                         rs.Close                              
                         if myfilesystem.FileExists ("c:\temp.txt") then
                              myfilesystem.DeleteFile "c:\temp.txt", True
                         end if
                         set mytextstream=myfilesystem.createtextfile("c:\temp.txt")
                         ctyStr=Request("ics")
                         mytextstream.WriteLine(ctyStr)          
                         mytextstream.Close    
                         set mytextstream = Nothing                              
                    %>
               </SELECT></TD></TR>
Nitin SontakkeDeveloper
CERTIFIED EXPERT

Commented:
I had rather resign to how http works and prefer to have a round trip. Or better still, consider revise the flow where the round trip behaviour looks more natural. This is recommended approach if data is too large.

If it is not too large, you can send whole of options of two list boxes and use javaScript for changing selection in second list box.

I understand what you are attempting but we have to understand that the ENTIRE asp processing is finished and it is only after that the user has control over page. When user inputs the asp processing STARTS ALL OVER AGAIN from start. And that's how http works, stateless.

Commented:
If you want to to a change in server side you can't do sequence between client side portions. You must make a client interface (a FORM with INPUTS and SELECTS) and then send information to server (SUBMITTING the form). Then, the server has to send results to client (creating a page in HTML code) and all again.

Client and server data transfers are synchronized with the creation on HTML pages (server to client direction) and submitting forms (client to server direction). You can't mix both of them, and try to make server and client actions sequencially in the same piece of code (for example, writting ASP functions and try they work with javascript events).

To understand this, try to imagine all code outside <% ... %> portions as a big comment. This is server point of view (they dump all these "comments" to client, without any interpretation).

Author

Commented:
OK, but my original question was how to create/ open etc a file on the server using javascript (I can do it ok using vbscript but want to be able to do it using JS.) The example I gave at the beginning is obviously wrong even though I got it from a JS reference book.  What am I doing wrong?
Nitin SontakkeDeveloper
CERTIFIED EXPERT

Commented:
Okay, let us give a try. Never done it before, honestly.

Will go step by step.

First, have you mentioned the language you are using in the beginning of your page? I am pretty sure you cannot mix the languages in single page.

Your page should start as

<%@Language="javaScript"%>

Author

Commented:
If I add <%@Language="javaScript"%> to the top of my asp page, the asp part does not work.  I changed the code to
<SCRIPT language = "javascript1.2">
<SERVER>
     var myFile = new File("/test2.txt");
     myFile.open("w");
     myFile.writeln("TEST");
     myFile.close();
</SERVER>
</SCRIPT>
and now it does not display what is written between the server tags, but it also does not create a file on the server.
regards
Nitin SontakkeDeveloper
CERTIFIED EXPERT

Commented:
Although, i am not too sure of what i am suggesting, would you mind changing your code as follows:

<SCRIPT Language="javascript1.2" RunAt="Server">
    var myFile = new File("/test2.txt");
    myFile.open("w");
    myFile.writeln("TEST");
    myFile.close();
</SCRIPT>

Please remember that now both of us are experimenting. As told you before i doubt if you can mix the languages. Is there any very particular strong reason why you don't consider using VBScript for this purpose?

Believe me, it's going to be a lot easier as you are alreading using VBScript for rest of your page.

One more small point, you are not suppose to give space on either side of = sign in html tags, as you have given one in following line.

<SCRIPT language = "javascript1.2">

Author

Commented:
What I am trying to do is the following:
I have an SQL database which has a list of jpg files, each file is associated with a country
f1.jpg japan
f2.jpg belgium
f3.jpg belgium
and so on
What I am trying to do is have to list boxes, the first with a list of the countries and the second with a list of image files.  I am trying to have the image file list change the contents dynamically depending on which country has been selected.  I have tried all sorts of things to do this and am now trying to write the name of a selected country to the server and then reload the page after reading the country name from the file and doing a SELECT FROM ... where country name like.  I have no problem writing and reading the file using vbscript, but I have great difficulty in reading the selected country so that I can write it to the file.  I can, however get the name without problem using JavaSript.  Sorry it is so complicated, perhaps I should just close this and keep on trying.  I would not have believed I would have had so much trouble.
regards
chris
Developer
CERTIFIED EXPERT
Commented:
See Chris,

First, I am having no trouble helping you out, so far. So please don't give up.

If you just decide to go in for VBScript, believe me, it is going to be very swift. Just tell me where exactly you are getting stuck. You are saying "but I have great difficulty in reading the selected country" what does that EXACTLY mean? Reading from file, from user input, where? Tell me and i will help you out.

If you are in UK, i can give you my email, phone, etc. If you want more interactivity.

Do reply, don't give up.

Author

Commented:
I have succeeded! I used javascript and two hidden select boxes which I used to filter out the images that I needed.  I am grately appreciative of your patience and will grade your answer A.  
chris

Explore More ContentExplore courses, solutions, and other research materials related to this topic.