我在MVC中工作,我试图从web服务器预览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路径
现代浏览器的默认安全设置不允许在具有src
属性的元素中使用file://
协议,包括iframe
(此处和此处的相关问题,另请参阅同源策略(。请使用服务器的相对路径,如下例所示。
控制器
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>