Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript Server Side Scripting....removing the %20 in links 2

Status
Not open for further replies.
Apr 9, 2007
26
0
0
US
Hi,

This page is built with Javascript as the server side scripting language. I inherited this and need to see how to remove the %20 in the link when I try to see in the status bar of the machine.

Not too sure if this is the right forum to ask server side Javascript questions.

In the page in the browser, I see folder contents which means, it is a display of all the Excel files in a particular folder of the drive name. So if I see in the folder path:

DeptWeb\QA\Docs\IQC

I see 6 Excel files which can written onto the browser using the "Scripting.FileSystemObject" and its methods.

When I point my cursor on the Excel file link "CMM for 1188 front gage RR", it shows me as:

file://servername/Deptweb/QA/docs/IQC/CMM%20for%201188%20front%20gage%20RR.xls

So obviously I get an error saying that "file://servername/Deptweb/QA/docs/IQC/CMM%20for%201188%20front%20gage%20RR.xls" is not found. Make sure the path or Internet address is correct.

How do I strip the %20 from the above link before I open the xls file in the browser?

Thanks.

The following writes the different files in the folder:
Code:
<li class="<% Response.write(fileExt); %>">123<a href="<% Response.write(fileLink); %>" title="<% Response.write(ftype); %>" target="_blank"><% Response.write(fileDisplay); %></a></li>

Code:
var fileLink = "file:///"+folderspec+"/"+filePath[filePathLen-1];
 
Not entirely sure I understand everything, but could you do something like this:

Code:
[blue]var[/blue] fileLink = [red]"file:///"[/red] + folderspec.replace([red]"%20"[/red], [red]" "[/red]) + [red]"/"[/red] + filePath[filePathLen-1].replace([red]"%20"[/red], [red]" "[/red]);

Ron Wheeler
Tekdev Open Source Development
 
Hi,

Thanks for the response. What this page attempts to do is to print out links which shows the name of the Excel files which are located in a server other than the web server. It shows the links fine. Users would click on the link and it should open the Excel file in a new page.

Just for testing purposes, When I response.Write to the browser:

Code:
var fileLink = "file:///"+folderspec+"/"+filePath[filePathLen-1];
					//var fileLink = "file:///" + folderspec.replace("%20", " ") + "/" + filePath[filePathLen-1].replace("%20", " ");
                    Response.Write (fileLink);

it looks like this:

file://///ExcelDocs_servername/DeptWeb/QA/docs/IQC/Gage R&R/CMM for 1188 front gage RR.xls

Wondering why those extra '//'?

The folder structure is exactly same. Only thing is that the folder structure is in a different machine (ExcelDocs_servername) and not in the web server.

When I point the cursor to the link, the status bar of the IE 7.0 browser, the link displays like this:

file://ExcelDocs_servername/Deptweb/QA/docs/IQC/CMM%20for%201188%20front%20gage%20RR.xls

Now when I go to the IIS, the folder structure for the folder 'docs' points to: Within docs, there are 2 folders

IQC where all the Excel documents are located.

\\ExcelDocs_servername\\DeptWeb\QA\Docs

Now when I click on the link, I get the error:

Cannot find "file://ExcelDocs_servername/Deptweb/QA/docs/IQC/CMM%20for%201188%20front%20gage%20RR.xls" Make sure the path or Internet Address is correct.

Hope I explained this better.
 
I used the unescape(string) command:

Code:
//var fileLink = "file:///"+folderspec+"/"+filePath[filePathLen-1];

					var fileLink = "file:///"+Unescape(folderspec)+"/"+Unescape(filePath[filePathLen-1]);

I get the error:

Code:
Microsoft JScript runtime error '800a138f' 

Object expected 

/_include_jscript/folderBrowse.asp, line 87

where line 87 refers to:

Code:
var fileLink = "file:///"+Unescape(folderspec)+"/"+Unescape(filePath[filePathLen-1]);

Thanks.
 
javascript is a case sensitive language, it is [!]u[/!]nescape, not [!]U[/!]nescape.

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
uncle_rico_thumb.jpg
 
lowercase unescape, not upper. BTW, thanks Ninjadeathmonkey.

[monkey][snake] <.
 
Now I do not get the Object Undefined error. But I still get the error pop up - "File not found:

I tried the following ways:

Code:
//var fileLink = ("file:///")+unescape(folderspec)+"/"+unescape(filePath[filePathLen-1]);

                    var fileLink = unescape(("file:///")+folderspec+"/"+filePath[filePathLen-1]);

I thought it could be the connection of the webserver with the docs_ServerName. But I can see all the files in the browser. It is only when I click on the file link, i get the error message.

What else can I try?

Thanks.
 
ninjadeathmonkey, yes I tried your approach and it errs with the wrong file name error.

I worked backwards. I hardcoded the URL:


The above link worked and it brought forward the Excel sheet in the browser.

My start page titled - Default.asp has this line:

Code:
<div class="folderContent"><% folderContents("//renoir/DeptWeb/QA/docs"); %></div>

The above is the docs servername where the docs are all located.

function folderContents is written in Javascript server side language:

Code:
function folderContents(fSpec) {
		var req = ""+Request.QueryString("f")+"";
		var req = (req != "undefined") ? req : "";
		var fRealName = req.split("_").join(" ");
		if (fRealName.indexOf("../") != -1) fRealName = "";
		var folderspec = fSpec+fRealName;

		
        var folderParent = ""+Request.ServerVariables("SCRIPT_NAME") + "?" + Request.ServerVariables("QUERY_STRING")+"";
		folderParent = folderParent.split("/");
		folderParentLen = folderParent.length;
		var temp="";
		for (x=1; x<folderParentLen-1; x++) {
            temp += "/" +folderParent[x];
		}
		folderParent = temp;

		var fso = new ActiveXObject("Scripting.FileSystemObject");
		var folder = fso.GetFolder(folderspec);
		var etFolders = new Enumerator(folder.SubFolders);
		var etFiles = new Enumerator(folder.files);

		if (etFolders.item()) { %>
			<h3>Folders</h3>
			<ul class="folderlist">
			<% if (req) { %>
				<li class="folder"><a href="<% Response.write(folderParent); %>" title="Go to the Parent Folder">[ GO UP ONE LEVEL ]</a></li>
			<%
			}

			// Show the subfolders in the folder
			for (; !etFolders.atEnd(); etFolders.moveNext()) {
				var folderString = String(etFolders.item());
				var folderPath = folderString.split("\\");
				var folderPathLen = folderPath.length;
				var folderDisplay = folderPath[folderPathLen-1];
				var folderLink = "?f=" + Server.URLEncode(fRealName.split(" ").join("_")) + "/" + Server.URLEncode(folderDisplay.split(" ").join("_"));
                
				%>
				<li class="folder"><a href="<% Response.write(folderLink); %>" title="Subfolder"><% Response.write(folderDisplay); %></a></li>
				<%
			}
			%>
			</ul>
		<%
		}

		else {
			if (req) {
			%>
			<h3>Folders</h3>
			<ul class="folderlist">
				<li class="folder"><a href="<% Response.write(folderParent); %>" title="Go to the Parent Folder">[ GO UP ONE LEVEL ]</a></li>
			</ul>
			<%
			}
		}

		// Show the files in the folder
		if (etFiles.item()) { %>
			<h3>Files</h3>
			<ul class="filelist">
			<%
			for (; !etFiles.atEnd(); etFiles.moveNext()) {
				var fileString = ""+etFiles.item()+"";

				if (fileString.indexOf("\\~$") == -1 && fileString.indexOf(".tmp") == -1 && fileString.indexOf("\\__") == -1) {
					var filePath = fileString.split("\\");
					var filePathLen = filePath.length;
					var fileExt = filePath[filePathLen-1].split(".")[filePath[filePathLen-1].split(".").length-1];
					var fileDisplay = filePath[filePathLen-1].split("_").join(" ").split("."+fileExt)[0];
					var fileLink = ("file:///")+folderspec+"/"+filePath[filePathLen-1]; //original line					
					//var fileLink = "file:///" + unescape(folderspec) + "/" + unescape(filePath[filePathLen-1]);
                    //[URL unfurl="true"]http://endoweb/departments/QA/Docs/IQC/Gage%20R%26R/Gage%20RR%20for%20Caliper%20.xls[/URL]
                    
					ftype="Unknown Document Type";
					if (fileExt == "asp") ftype="Active Server Page";
					else if (fileExt == "asx") ftype="Streaming Windows Media Audio";
					else if (fileExt == "bmp") ftype="Bitmap Image";
					else if (fileExt == "css") ftype="CSS Stylesheet";
					else if (fileExt == "doc") ftype="Microsoft Word Document";
					else if (fileExt == "exe") ftype="Application";
					else if (fileExt == "fla") ftype="Flash Document";
					else if (fileExt == "gif") ftype="GIF Image";
					else if (fileExt == "htm" || fileExt == "html") ftype="HTML Document";
					else if (fileExt == "jpeg" || fileExt == "jpg") ftype="JPEG Image";
					else if (fileExt == "mov") ftype="QuickTime Movie";
					else if (fileExt == "mp3") ftype="MP3 Audio File";
					else if (fileExt == "mpp") ftype="Microsoft Project File";
					else if (fileExt == "msi") ftype="Microsoft Installer";
					else if (fileExt == "pdf") ftype="Adobe PDF Document";
					else if (fileExt == "png") ftype="PNG Image";
					else if (fileExt == "ppt") ftype="Microsoft Powerpoint Presentation";
					else if (fileExt == "rtf") ftype="Rich Text Format";
					else if (fileExt == "swf") ftype="Flash Movie";
					else if (fileExt == "txt") ftype="Plain Text File";
					else if (fileExt == "vsd" || fileExt == "vsx") ftype="Microsoft Visio Document";
					else if (fileExt == "vcf") ftype="vCard Contact Information";
					else if (fileExt == "vcs") ftype="Outlook vCalendar File";
					else if (fileExt == "wma") ftype="Windows Media Audio";
					else if (fileExt == "xls") ftype="Microsoft Excel Spreadsheet";
					else if (fileExt == "xml") ftype="XML Document";
					else if (fileExt == "zip") ftype="Compressed ZIP File";
                    
					%>
					<li class="<% Response.write(fileExt); %>"><%Response.Write (folderspec); %><a href="<% Response.write(fileLink); %>" title="<% Response.write(ftype); %>" target="_blank"><% Response.write(fileDisplay); %></a></li>
					<%
				}
			}

			%></ul>
			<%
			
		}
	}
%>

Is there a way I convert the file location to the virtual directory? Because when I type in the virtual directory directly, then the XLS file opens up.

THanks.
 
Hi,

I worked backwards. Using the file path, I created Right and Left functions and then replaced it with the virtual directory. Now the link points to the virtual directory and shows the excel file.

Thanks all for the help you have suggested and given.

Regards.
 
I am revisiting this again :(

Please suggest me how to rewrite this to get my desired results:

Code:
var fileLink = ("file:///") + folderspec + "/" + filePath[filePathLen-1]; //original line

Above line give the following result:
Code:
file://///renoir/DeptWeb/QA/docs/IQC/IQC Mission Statememt/IQC Mission Statement.pdf

Code:
var fileLink1 = Left(fileLink,24) //stripping the link with the file domain.

Above line gives me the following result:
Code:
file://///renoir/DeptWeb

Code:
var fileLink2 = fileLink1.replace(fileLink1,"[URL unfurl="true"]http://endoweb/departments/QA/Docs/");[/URL] //replace it with the virtual directory

Above line gives me the following result

Code:
[URL unfurl="true"]http://endoweb/departments/QA/Docs/[/URL]

Now what I want to do is to take the length of 'fileLink', then take the length of the stripped of file domain - 'fileLink1". Which I can do by using the 'length' property. Now the difference of the 2 length should return me the folder path. Am I right?

Now what is the synatax to get the folder path after stripping off the file domain?

Code:
var folderToAppend = (fileLink.length - fileLink1.length);

But the above code returns me a numric value of 60.

How do I get the string value?

Any ideas?

Thanks.
 
Now the difference of the 2 length should return me the folder path. Am I right?

No. The .length function returns an integer value. you need the substring of the difference (substr).

What do you consider to be the folder path? Type it as a string and I'll show you how to go about getting it.



[monkey][snake] <.
 
This is what is it originally:

file://///renoir/DeptWeb/QA/docs/IQC/IQC Mission Statememt/IQC Mission Statement.pdf

I did the following steps:

1. Took the length of the above string stored ina variable fileLink
2. Stripped of the file domain: file://///renoir/DeptWeb
3. Replace the 1st string with "4. Now I want to append the folder to the string in No. 3

So it should now look like this:

Mission Statememt/IQC Mission Statement.pdf

But I do not want to hardcode the folder. Whatever ever is left after stripping the file domain is to be appended to the http URL.

Thanks.
 
This example is good one in illustrating what I want to do:

Original: file://///renoir/DeptWeb/QA/docs/IQC/Gage R&R/CMM for 1188 front gage RR.xls

Strip Domain: file://///renoir/DeptWeb/QA/docs/

New Domain:
NewLink: R&R/CMM for 1188 front gage RR.xls

Thanks.
 
This is what you would need to do:

Code:
var folderToAppend = fileLink.replace(filelink1, "");

That will give you the folder path.

[monkey][snake] <.
 
Thanks a lot.

I had done this and replaced with your code. I wanted to do away with hardcoding of the index number.

Code:
//var folderToAppend = fileLink.substring(33, fileLink.length); //folder to append
					var folderToAppend = fileLink.replace(fileLink1, ""); //folder to append

Now I can open all my files.

THanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top