简体   繁体   中英

Display File From Local Drive in MVC view

I have been trying to display local Pdffile from D drive in a view of MVC along with other data.I have tried out Iframe and even added extensions for browsers like local links to display but of no use.I have been stuck with this problem since 3 days.I have tried out the following code.

<iframe src="@Url.Content("file:///D:/PdfsFolder/" + Model.FileName)"></iframe>

This works fine for me in IE but not working in Other browsers.When I try to open only the file using hyperlink it works in all browsers.My problem is to display it along with other data.please help me out If there is any other way to display that file in the View other than Iframe.

you have to write a controller & action that will fetch the file and pass back to the response:

public ActionResult TestPdf()
{
    return File(@"d:\test.pdf", "application/pdf");
}

and now in your view you could use an iframe to point it to this controller action:

<iframe src="<%= Url.Action("TestPdf", "SomeController") %>"></iframe>

为什么要尝试从驱动器附加文件,请使用项目的根目录来显示文件,例如在项目中创建文件夹,然后将其放入文件并尝试以下代码

<img src="<%= Url.Content("~/Content/UserImages/FileName.jpg") %>" />

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