1

i am having problem in calling javascript function
I want to open a file on click of a link.

Here is my code:

<script language="javascript" type="text/javascript">
var refViewer = null;

function OpenViewerWindow(image) {
    return window.open(image, "Viewer", "height=400px,width=550px,menubar=no,scrollbars=yes ,resizable=yes,top=100px,left=234px");

}


function openViewer(image) {

    if (refViewer != null) {
        if (refViewer.closed == false) {
            refViewer.close();
            refViewer = OpenViewerWindow(image);
        }
        else refViewer = OpenViewerWindow(image);
    }
    else
        refViewer = OpenViewerWindow(image);
}   


</script>

<a onclick=javascript:openViewer(@ViewBag.path)><img src="pic.jpg"/></a>

and the in the controller:

 public ActionResult ActivityPosting(int HobbyDetailID)
    {
string filepath = Server.MapPath("~/ePortfolio/PortFolioContent/" + HobbyDetailID + "/ReferenceMaterial/" + item.FilePath);
 ViewBag.path = filepath;
    return view();
   }

The problem is the javascript function does not get called.Please help me

2 Answers 2

0

Try this instead in your JavaScript OpenViewerWindow function (you need to use the file:// protocol:

return window.open("file://" + image, "Viewer", "height=400px,width=550px,menubar=no,scrollbars=yes   ,resizable=yes,top=100px,left=234px")
Sign up to request clarification or add additional context in comments.

7 Comments

the problem is openViewer() function itself is not getting called
Try changing your a tag to be this: <a onclick="openViewer('@ViewBag.path');"><img src="pic.jpg"/></a>
Can you tell us your exact filepath in the @ViewBag.path variable that the window is trying to open? That must be wrong.
ya the file path is:E:\HOBBYHOMES(E-PORTFOLIO)\ePortfolio\e-Portfolio\PortFolioContent\1\ReferenceMaterial\prtf_1.docx
Ah try my edit, it must start with file:/// not file:// what I put originally :)
|
0

Make sure you pass a string:

<a onclick=javascript:openViewer('@ViewBag.path')><img src="pic.jpg"/></a>

Notice the single quotes. Or even better use Json.Encode to ensure proper encoding of the value that is passed to your openViewer javascript function:

<a onclick="javascript:openViewer(@Html.Raw(Json.Encode(ViewBag.path)))"><img src="pic.jpg"/></a>

Also there's another issue with your code. You use Server.MapPath to calculate the url but this method returns the absolute path to the file on the server. The client obviously cannot access it with an absolute path. You should pass an url using the Url.Content helper:

public ActionResult ActivityPosting(int HobbyDetailID)
{
    string filepath = Url.Content("~/ePortfolio/PortFolioContent/" + HobbyDetailID + "/ReferenceMaterial/" + item.FilePath");
    ViewBag.path = filepath;
    return View();
}

3 Comments

Oops, that's because I forgot to set ViewBag.path = filepath; in my example. I have updated.
the function is getting called but the file is not opening
That's another problem with the url that you are requesting. Maybe there's some issue with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.