...

View Full Version : File reading help



ahosang
10-07-2003, 10:18 PM
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);

whammy
10-13-2003, 02:22 AM
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:


// Compress spaces
String.prototype.compress = function()
{
return this.replace(/\s+/g,' ');
}


Server-side you can just use something like:


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. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum