简体   繁体   English

使用 Razor 和 javascript 在新窗口中打开图像

[英]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由于错误的代码编辑了函数 openImage

解决方法是通过这种方式获取保存图片的目录的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.可能是ViewBag.Path缺少/ To get around this, don't concatenate ( + ) url parts, but use System.IO.Path.Combine , as in:要解决此问题,请不要连接 ( + ) url 部分,而是使用System.IO.Path.Combine ,如下所示:

@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)您可以更改循环以创建指向新窗口/选项卡的简单链接(这些也可以通过键盘导航),而不是使用 javascript onclick

@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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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