简体   繁体   中英

Open image in a new window using Razor and javascript

I'm trying to open in a new window a new full size image from each one. I'm using this code:

<script type="text/javascript">
function openImage(imageUrl) {
    window.open(imageUrl);
}

@foreach (var image in Model.Images)
{
        <img src="@Url.Action("Thumbnail", "Done", new { width = 150, height = 150, file = @imagen.filename })" alt="@imagen.filename" onclick="openImage('@Url.Content(ViewBag.Path + @imagen.filename)')" />
}

But using this when I click on a image it can't open anything.

Can you help me with this issue?

Thank you in advanced

EDITED the function openImage due to wrong code

解决方法是通过这种方式获取保存图片的目录的url: string url = Url.Content("~/Images");

There's a number of mismatched variable names in the question

imageUrl/imgUrl
image/@imagen

I'll assume these are typos for the question.

Could be that ViewBag.Path is missing a / at the end. To get around this, don't concatenate ( + ) url parts, but use System.IO.Path.Combine , as in:

@Url.Content(Systemm.IO.Path.Combine(ViewBag.Path, image.filename))

Rather than using javascript onclick , you could change your loop to create simple links to new windows/tabs (these would also be navigable via the keyboard)

@foreach (var image in Model.Images)
{
    <a target="_blank" href='@Url.Content(ViewBag.Path + @image.filename)'>
        <img src="@Url.Action("Thumbnail", "Done", new { width = 150, height = 150 })" alt="@image.filename" />
    </a>
}

If this doesn't work (and I'm expecting it won't for you), please provide details for:

alert('@ViewBag.Path')

function openImage(imageUrl) {
    alert(imageUrl);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM