Solved

How to dynamically build a file name to provide the src path to an iframe

Posted on 2009-05-11
10
189 Views
Last Modified: 2012-05-06
In Javascript, I have been trying to provide a filename to the src property of an iframe.  
When I hard-code the filename, it works just fine. But when I try to concatenate the path, it doesn't.
Please help.  
filePath = "devhilvs09\\Data\\Forms\\j50gmqeqtlpv.DOC"

filename= "file://" + filePath;

//this works:

document.getElementById('docIFRAME').src = "file://devhilvs09\\Data\\Forms\\j50gmqeqtlpv.DOC";

//this doesn't:

document.getElementById('docIFRAME').src = filename;

Open in new window

0
Comment
Question by:HellaSym
  • 6
  • 4
10 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24359505
HellaSym,

If you alert filename (or print it somehow) then what do you see?  It seems the results should be the same between the 2 lines.

Let me know if you have any questions or need more information.

b0lsc0tt
0
 

Author Comment

by:HellaSym
ID: 24359607
In the alert, I see the path as follows:
file://devhilvs09\\Data\\Forms\\j50gmqeqtlpv.DOC
Yet, when it goes to the iframe, an error comes up:
Cannot file 'file://\\devhilvs09\\Data\\Forms\\j50gmqeqtlpv.DOC. Make sure the path or Internet address is correct.

It adds 2 back-slashes on its own...
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24359735
Thanks for that info.  What exactly did you alert?  What if you try to just "write" the filename value (i.e. just use innerHTML to put it in some div)?  What browser and version of the browser are you using?
bol
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24359747
Also please let me know if the code you showed isn't really how you are doing this.  The code you posted should not add the backslashes.
bol
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24359780
Also what is the protocol of the URL you are using on the main page?  Is it also file: or is it http:?  Is it localhost or on an Internet page?
bol
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:HellaSym
ID: 24359850
I did alert on filename: alert(filename);
Displaying the filename into a text box displayed it correctly.

I am running on IE 6.0, Version:6.0.2900.2180.xpsp_sp2_qfe.090206-1239    

Here is the code i am running:
// the file name comes back from the server (c#)
                    byte[] byteArray = null;
                    byteArray = ((DesktopDocument)doc).DesktopFileData;

                    using (BinaryWriter bw = new BinaryWriter(File.Open(tmpFile, FileMode.Create)))
                    {
                        bw.Write(byteArray);
                        bw.Close();
                    }
                // remove the leading "\\\\"
                    string fullPath = Path.GetFullPath(tmpFile).Substring(2).ToString();
                    return fullPath;

// returns the filename to the client side (Javascript)
filename = filename.replace(/\\/g,"\\\\");
filename = "file://" + filename;
document.getElementById('FileName').value = filename;
alert(filename);
document.getElementById('docIFRAME').src = filename;

The iframe is part of a web page running on the localhost.  So, yes the protocol is http:// on the main page.

Thanks.
0
 

Accepted Solution

by:
HellaSym earned 0 total points
ID: 24366311
I replaced the double-backslashes with a single forward slash in the path and it worked.

Thanks for your time!
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24366885
Thanks for the response and info.  I need to look over the code more but can you test this on IE7?  It was IE7 that I used (and FF) when I tested and I couldn't duplicate it.  I am curious if IE7 will work OK for you.  That would let us know it is an IE6 issue and then I will try to find IE6 to test and work with.
bol
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24366906
I just noticed your second post.  Glad you got it working.  Let me know if you need any more help or have a question about closing this.
Was it the replace line you changed (in the code you just posted) or some other part of your page you changed?  If it was the replace then it seems the issue was something not shown in the original question.  I am glad you looked at the other parts and found the problem.
bol
0
 

Author Comment

by:HellaSym
ID: 24366938
Yes, I changed the replace line.  However the replace line was there to put back double-backslashes in the path, which Javascript replaced with single backslashes when the path came back from the server.

In any case, I am relieved it works now.  Thanks again.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now