Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Sep 2002
    Location
    Surrey, UK
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    File reading help

    I am trying to send back a text/javascript response(it's a dynamically loaded <script> tag) to the user. The variable HTML will end up as a javascript string representing the contents of a multi-line file(probably just a .html file, could be .pdf). Please can someone fill in the part that reads off the file on the server. Please remember that it's important to strip all returns, line separator because these would cause error in javascript.
    Oh, and I don't practise ASP, only JSP, but I got this far from a book. Any other errors, please point out.

    Here's my attempt.
    getdata.asp:
    <% Response.ContentType="text/javascript"%>
    var div="<%= Request.QueryString("div") %>";
    <% Dim HTML=""
    Dim url=Request.QueryString("url")
    Set file=Server.CreateObject("System.FileSystemObject")
    Set fileContents=file.getFile(url)**NOTES - NOW I'M NOTE SURE - MAY HAVE TO USE BINARY READ OR LINE-BY-LINE READING**
    ** MAYBE EVEN TEXTSTREAM OBJECT - NEED TO ASK AN ASP EXPERT TO DO THIS PART**
    ** AFTER YOU OBTAIN THE FILE CONTENTS WITHOUT LINE SEPARATORS, SET 'HTML' VARIABLE TO THE FILE CONTENTS, IT ENDS LIKE BELOW**
    %>
    var HTML="<%= HTML %>";
    doUpdate(div, HTML);

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmm, are you just having trouble reading a text file (or whatever) into a javascript variable?

    First of all I wouldn't call your FSO object "file", since that's confusing. I'd call it "fs" or "FileSystem" or something like that. W3Schools.com consistently uses "fs" to refer to the filesystem, so I got into that habit when I learned it.

    Then you could use "f" or "file" as your file reference... I'd suggest "f" since "file" is a property of some objects, and that could be confusing as well.

    If you're confused about reading files from the server, just go through the examples at:

    http://www.w3schools.com/asp/asp_ref_filesystem.asp

    ... and that should clear everything up for you. Once you do that, you should be able to use:

    myvariable = f.ReadAll

    Then you can just strip any extra returns out either server-side or client-side. Here's a client-side example:
    Code:
    // Compress spaces
    String.prototype.compress = function()
    {
    	return this.replace(/\s+/g,' ');
    }
    Server-side you can just use something like:
    Code:
    Function RemoveExtraSpaces(ByVal str)
    	If IsNull(str) Then Exit Function
    	Dim resRegEx
    	Set resRegEx = New RegExp
    	resRegEx.Pattern = "\s+"
    	resRegEx.Global = True
    	RemoveExtraSpaces = resRegEx.Replace(str," ")
    End Function
    Hope this helps.
    Last edited by whammy; 10-13-2003 at 03:25 AM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •