Solved

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

Posted on 2009-05-11
10
191 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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
 

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

831 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