Display File From Local Drive in MVC view - asp.net-mvc

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>

why are to trying to attach a file from drive use root directory of project to pic the files like create a folder in your project put files their and try below code
<img src="<%= Url.Content("~/Content/UserImages/FileName.jpg") %>" />

Related

Display file in an Iframe MVC Razor

My problem here is I have been trying to display a pdf file in an Iframe.It works fine in IE but not working in chrome or firefox.However I am able to open them in a new tab.But as per my requirement I have to display it along with other form where data from pdf will be filled in the form.So I have used Iframes.Is there any other solution to display file along with form other than Iframes.what can i do with it? Below is the one which I tried.
<iframe src="file://///GSEV/PdfsFolder/#Model.FileName"></iframe>
<iframe src="#Url.Content("file://///GSEV/PdfsFolder/" + Model.FileName )"></iframe>
thanks in advance..

How do I return MVC File ActionResult of PDF over jQuery modal dialog

Note: I am not wanting to display the PDF inline with the modal. Rather, I am looking to have the browser acknowledge the file and allow the user to save or open it.
I have a jQueryUI modal dialog in MVC 4. The dialog IS modal. The content of the dialog is from a Partial Views which works fine. I have only one button on the dialog itself and have successfully gotten all the JavaScript to deal with client side data entry checking.
What is giving me a headache is that I have one button embedded in the Partial View that is to display a PDF file. I can successfully call a JavaScript function that calls the controller that gets the file from another server. I can even get the file converted to a byte array and the last line is
return File(contents, "application/pdf", "PropsedChanges.pdf");
However it will not open as I suspect that the modal dialog is preventing it.
I have done something similar outside of the model dialog and it gives me the save/open option at the bottom of the screen for IE or in the correct manner in other browsers.
Is there a way to display the PDF in a registered PDF viewer on the client's PC/Device outside of the browser needing to ask if they want to save or open it? Which, as I suspect, is not happening due to the modal dialog.
Help is greatly appreciated.
public ActionResult GetPdf(Model modelo)
{
return File(Pdf bytes[], "application/pdf");
}
Try using a new window to open your PDF. Like target="_new" or "blank".
If you are using Asp.net 5, install MvcPdfActionResult via nuget
PM> Install-Package MvcPdfActionResult
Simply use return type as PdfActionResult in the controller will output PDF document instead of HTML. This converts HTML to PDF using the iTextXmlWorker Library.
...
return PdfActionResult(model);
}
Generates pdf documents from your razor views within an asp.net 5 MVC project. https://www.nuget.org/packages/MvcPdfActionResult/

Display file (PDF/TXT) on form brought from Database in MVC ASP.NET

I want to display file on my form using MVC.
I am bringing a Byte[] array data from the Database, and using FileContentResult I am converting it into a file.I want to now display this file on my page for viewing . How can it be acheived. What code to write in my View for the same.
Assuming you're using Razor, rendering a text file can be done as simple as:
<div>
#(new System.IO.StreamReader("myFile.txt")).ReadToEnd()
</div>
For PDF files, you'll have to find a third-party component to convert to HTML.
You probably don't want to use FileContentResult, that is something generally used for providing the raw file.
In theory though there is nothing different in using any other url
<img src="#Html.ActionLink("View","Image",{id = Model.key})" />
Or you can provide that link in a pdf reference, or as a stylesheet etc.

Serving static content to my action using MVC's convention approach

I'm looking at outsourcing some in-page help on a large web application I am creating and would like to make it really easy for this content to added to our pages when we're ready for it.
So I was thinking I could create a system where I can add text files to the same folder where an action expects it's view to be and read out the content of that file the content in that file can be passed back to the view to display. Either that or create a helper that would do the same.
Example
Controllers
HomeController.cs
Views
Home
Index.aspx
Index.txt
Index.aspx would then have access to the content in Index.txt.
How would I start going about creating this system. Are there any built in classes in .NET MVC that I could take advantage of?
A similar question was asked yesterday: Including static html file from ~/Content into ASP.NET MVC view.
Basically you can read the text from the file and include it inside your view by using File.ReadAllText so you would have something like this inside your index.aspx file
<%= File.ReadAllText(Server.MapPath("~/Views/Home/index.txt")) %>
I'd create a parallel hierarchy in the Content folder and put the files there, probably as HTML. Then you can simply load them via AJAX in the view using the parallel hierarchy convention.
Content
Help
Home
index-help.html
about-help.html
Foo
index-help.html
bar-help.html
Then in your views
<div class="help">
<noscript>
<a href='#Url.Content( "~/content/help/home/index-help.html" )'>Click for Help</a>
</noscript>
</div>
<script type="text/javascript">
$(function() {
$('.help').load( '#Url.Content( "~/content/help/home/index-help.html" )' );
});
</script>
You may also be able to extract the controller/action from RouteData in the view if your routes are consistent and move this to your _Layout.cshtml file with the path being provided by route data.
#{
var controller = ViewContext.RouteData["controller"] as string;
var action = ViewContext.RouteData["action"] as string;
var url = Url.Content( string.Format( "~/content/help/{0}/{1}-help.html", controller, action ) );
<div class="help">
<noscript>
<a href="#url>Click for Help</a>
</noscript>
</div>
<script type="text/javascript">
$(function() {
$('.help').load( "#url" );
});
</script>
}
One possible solution would be to store them as xml file instead, that are serialized from the model the view is expecting. You could then create an Action Filter populate the model being returned with the data from the XML file. I hope that helps.

Determine an absolute url to a resource using vbscript/classic asp

I've created a pseudo user control for a site written in classic asp. The control is simply an asp page (with full HTML headers and body) that resides within an iframe in the parent page. The point was to create an AJAX-like interface for uploading files asynchronously (the parent page contains a large form and I didn't want to have to upload the files and submit the rest of the form at the same time).
The problem is, I'm running into a lot of issues with relative urls being used in the iframe page/user control. Depending on what page the iframe is a child of, the relative url base location seems to change according to the directory that particular page is in.
Example:
www.website.com/directory1/application1.asp
...
<form>
<input>
...
<iframe src="../controls/FileUpload.asp"/>
...
</form>
...
www.website.com/directory1/directory2/application2.asp
...
<form>
<input>
...
<iframe src="../../controls/FileUpload.asp"/>
...
</form>
...
www.website.com/controls/FileUpload.asp
...
<form method="post" enctype="multipart/form-data" action="FileUpload.asp"><!--problem here-->
<input type="file">
<input type="submit"/>
</form>
The iframe src paths work correctly (notice the one that's buried a directory deeper has an extra double dot). But in the code for the FileUpload.asp page, relative URLs don't work consistently. The URL I have in the action attribute for the form tag works if you simply load the page as-is, not in an iframe of another page. You can change it to "../controls/FileUpload.asp" and it will work on the first application page, but you have to add another "../" for it to work on the second application page.
I was wondering if maybe there's a way with vbscript to find the absolute URL to a certain file. I do use an include file into which I could hard-code this, but I'd rather not if that's possible. Any other ideas?
You could also just put in an absolute path from the root such as
action="/controls/FileUpload.asp"
I'm not sure if you are perhaps looking for
<%
Response.Write Server.MapPath("./foo.txt")
%>
Some usefull code from Thorarin
that I just saw in a different post
Look for ThisPage() Function

Resources