无法打开锚链接指向在Chrome到文件 [英] Cannot open anchor links pointing to file in Chrome

查看:174
本文介绍了无法打开锚链接指向在Chrome到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的ASP MVC 3的网站,我们有一些超链接对文件指出我们的局域网上(这是一个内部网站)。这些链接一直工作到今天为止..此时没有一个会工作。此前今天你能够点击链接和文件(通常是一个.XLSX US preadsheet)上会打开。

的文件路径被保存在数据库中的一个字段。在索引视图,如果附件字段的内容,然后用一个回形针图像的超链接在该表格单元格中。下面是从视图中的code:

 < TD>
        @if(!String.IsNullOrWhiteSpace(item.Attachment))
        {
            &下;一个href=@item.Attachment类=啉>&下; IMG SRC =@ Url.Content(〜/内容/图像/ attachment.png)高度=20像素;宽度=20像素;/>&下; / A>
        }
    < / TD>

这是此HTML是如何呈现的例子

 < TD>
    <一href=\"\\\\Prdhilfs03\\l&i-sales&mkt\\WORKAREA\\Agencyservices\\Shared\\AIC\\MasterListAttachments\\AF.xls\"类=啉>&下; / A>
    < IMG SRC =/内容/图片/ attachment.pngHEIGHT =20像素; WIDTH =20像素;>
< / TD>

如果我点击右键并复制链接地址,这是我得到我的剪贴板

<$p$p><$c$c>file://Prdhilfs03/l&amp;i-sales&amp;mkt/WORKAREA/Agencyservices/Shared/AIC/MasterListAttachments/AF.xls

不过,如果我右键点击该链接,选择检查元素,然后右键从控制台点击锚链接并选择复制链接地址我得到这样的:

<$p$p><$c$c>http://localhost:2574///Prdhilfs03/l&amp;i-sales&amp;mkt/WORKAREA/Agencyservices/Shared/AIC/MasterListAttachments/AF.xls

任何帮助或建议将是AP preciated。谢谢!

修改

当我点击链接,我在Chrome控制台收到此错误信息:

 不允许加载本地资源: file://Prdhilfs03/l&amp;i-sales&amp;mkt/WORKAREA/Agencyservices/Shared/AIC/MasterListAttachments/AF.xls


解决方案

我会添加到您正在运行到跨站点脚本问题卡尔的回答。你有另一种方法是有一个控制器有问题的文件返回作为FileResult。让我们把这个FileRouter。然后FileRouter将会有一个动作叫下载,这样的事情:

 公共FileContentResult下载(字符串路径)
{
    返回this.File(/ *的流路径* /);
}

您会然后传递路径,控制器上的行动。因此,而不是直接的联系,你会用放置intranetserver / filerouter /下载的目标的Html.Action链接?路径=文件路径

当然,这具有服务器加载关闭网络不必要的文件的缺点,但它是一个需要考虑的替代品。

此外,使用FilePathResult可以在这里工作为好,但我没有任何经验,所以它不能使放在这里它的功效任何索赔要求。

In our ASP MVC 3 site, we have some hyperlinks that point towards files out on our LAN (this is an intranet site). These links have ALWAYS worked up until today.. at which point not a single one will work. Prior to today you were able to click on the link and the file (usually an .xlsx spreadsheet) would open up.

The file path is saved as a field in the database. On the Index view, if this attachment field has content then a hyperlink with a paperclip image is used in this table cell. Here is the code from the view:

    <td>
        @if (!String.IsNullOrWhiteSpace(item.Attachment))
        {
            <a href=@item.Attachment class="noLine"><img src="@Url.Content("~/Content/Images/attachment.png")" height="20px;" width="20px;"/></a>
        }            
    </td>

And here is an example of how this HTML is rendered

<td>
    <a href="\\Prdhilfs03\l&amp;i-sales&amp;mkt\WORKAREA\Agencyservices\Shared\AIC\MasterListAttachments\AF.xls" class="noLine"></a>            
    <img src="/Content/Images/attachment.png" height="20px;" width="20px;">
</td>

If I right-click and copy the link address this is what I get on my clipboard

file://Prdhilfs03/l&amp;i-sales&amp;mkt/WORKAREA/Agencyservices/Shared/AIC/MasterListAttachments/AF.xls

However, if I right click the link, select "Inspect Element", then right click the anchor link from the console and select "Copy Link Address" I get this:

http://localhost:2574///Prdhilfs03/l&amp;i-sales&amp;mkt/WORKAREA/Agencyservices/Shared/AIC/MasterListAttachments/AF.xls

Any help or suggestions would be appreciated. Thanks!

EDIT

When I click the link, I receive this error message in the Chrome console:

Not allowed to load local resource: file://Prdhilfs03/l&amp;i-sales&amp;mkt/WORKAREA/Agencyservices/Shared/AIC/MasterListAttachments/AF.xls

解决方案

I'll add to Karl's answer that you are running into the cross site scripting issue. An alternative you have is to have a controller that returns the file in question as a FileResult. Let's call this a FileRouter. The FileRouter will then have one action called Download, something like this:

public FileContentResult Download(string path)
{
    return this.File(/* a stream to the path */);
}

You would then pass the paths to the action on your controller. So instead of a direct link, you'll place an Html.Action link with a target of intranetserver/filerouter/download?path=path_to_file

Of course this has the drawback of the server loading the file off the network unnecessarily, but it's an alternative to consider.

Additionally, using a FilePathResult may work here as well, but I don't have any experience with it so can't make any claims on its efficacy here.

这篇关于无法打开锚链接指向在Chrome到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆