Link to home
Start Free TrialLog in
Avatar of HellaSym
HellaSym

asked on

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

Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

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
Avatar of HellaSym
HellaSym

ASKER

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...
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
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
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
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.
ASKER CERTIFIED SOLUTION
Avatar of HellaSym
HellaSym

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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
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.