【问题标题】:Not allowed to load local resource: file:// using iframe in MVC不允许加载本地资源:file:// 在 MVC 中使用 iframe
【发布时间】:2018-08-06 11:00:27
【问题描述】:

我在 MVC 中工作,我正在尝试从网络服务器预览 pdf,但它显示错误:

不允许加载本地资源:file://web-server/images/1324/Sample%20Document.pdf

<iframe src="@Model.urlpath" width="650" height="350" style="border: 1px solid #ccc; margin:10px 5px !important;"></iframe>

我正在尝试获取从 Web 服务器到本地应用程序的 url 路径

【问题讨论】:

  • HTML embedded PDF iframe的可能重复
  • @Model.urlpath 中包含什么 URL 字符串?如果知道 URL 字符串,问题应该很容易解决。

标签: asp.net-mvc iframe


【解决方案1】:

现代浏览器的默认安全设置不允许在具有src 属性的元素中使用file:// 协议,包括iframe(相关问题herehere,另请参阅same origin policy)。请改用服务器的相对路径,如下例所示。

控制器

var model = new ViewModel();
model.urlpath = string.Format("~/images/{0}/{1}", "1324", "Sample_Document.pdf");

查看

<iframe src="@Url.Content(Model.urlpath)" width="650" height="350" style="border: 1px solid #ccc; margin:10px 5px !important;"></iframe>

或者在 @Url.Content 中使用硬编码的虚拟路径,然后从 viewmodel 属性中传递文件名:

控制器

var model = new ViewModel();
model.FileName = "Sample_Document.pdf";

查看

<iframe src="@Url.Content("~/images/1324" + Model.FileName)" width="650" height="350" style="border: 1px solid #ccc; margin:10px 5px !important;"></iframe>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-23
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    • 2016-11-04
    相关资源
    最近更新 更多