[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

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

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
HellaSym
Asked:
HellaSym
  • 6
  • 4
1 Solution
 
b0lsc0ttCommented:
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
 
HellaSymAuthor Commented:
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
 
b0lsc0ttCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
b0lsc0ttCommented:
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
 
b0lsc0ttCommented:
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
 
HellaSymAuthor Commented:
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
 
HellaSymAuthor Commented:
I replaced the double-backslashes with a single forward slash in the path and it worked.

Thanks for your time!
0
 
b0lsc0ttCommented:
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
 
b0lsc0ttCommented:
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
 
HellaSymAuthor Commented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now