I’m trying to put together a MVC app that isn’t a typical create a record and present the record to the user.
I’m finding some things that aren’t friendly in MVC as they are in Web Forms. My view has two dropdowns, a textbox, and a submit button. In one of the dropdowns I have to prepopulate it with codes and description. That part is done.
Next, I have the user enter text into the textbox. They click on a Find button. Find will populate the 2nd dropdown. Without viewstate, the code is a bit different but possible. Next, the user clicks on the submit button. Here is the tricky part. I need input from the view to generate a PDF file, then open a new browser window or tab to display the PDF.
I also want to delete the PDF from the server before presenting it. I can delete the PDF before presenting it in web forms. I found Actionlink can open a new window, but Actionlink doesn’t push the input on the view to the controller. A standard form submit button does, but a submit button doesn’t open a new window. A controller cannot open a view in a new window either.
How do I get the users input and push that input to the controller, and then display the PDF generated into a new browser window? On top of that, I need to delete the PDF off the server.
Simple create an Action that returns your Pdf stream as a FileResult
If you are generating your pdf to a memory stream: (I recommend this):
public ActionResult DownloadPdf()
{
// you need some code here to generate the pdf to the memory stream.
return File(stream, "application/pdf", "DownloadName.pdf");
}
Or if you prefer, use the filepath directly :
public ActionResult DownloadPdf()
{
// get pdf filepath
var path = "Chap0101.pdf";
return File(path, "application/pdf");
}
Then in your html code you can use something like this to open in a new window.
#Html.ActionLink("Download pdf in new window", "DownloadPdf", "ControllerName", null, new { target = "_blank" })
You might return a view with some javascript that first opens a new window and then refreshes to another view. If no javascript is active, the view wouldn't reload and a link would be there to open manually. If you create the PDF in memory, there is nothing you need to delete, just stream the in memory PDF to the client. I'm new to MVC myself, this is just an idea.
Related
I have one "Download PDF" Image link, I am calling an action of a controller in order to allow users to download specific file from external site (so has given complete URL of PDF file link)
I have written following code, but its not working.
public virtual ActionLink OpenPDF()
{
string fileName = "http://mysite/filetodownload.pdf";
return File(fileName, "application/pdf", Server.UrlEncode(fileName);
}
This controller action gets called from an Image link.. and I can see this action gets called..
When I click on image, the code gets executed, and asks to Open/Save file, but when I say Save it says "This file cannot be downloaded"
what do you think can be wrong here.
Why don't you just point your link directly to the site
Download File
You don't need to go through a controller for this
As a side not, if you are returning a FileResult you need to pass it a stream, a byte array, or a path to a file on disk. You can't pass it a third party URL. It doesn't work like that. It is meant to work like this:
public virtual ActionLink OpenPDF()
{
string fileName = Server.MapPath("~/Download/filetodownload.pdf");
return File(fileName, "application/pdf");
}
I think
Show images in table from database in Asp.net-mvc3
Azure blobs and thumbnails
ASP.NET MVC - user managment of folder with pictures (FTP?)
links as you meet the answer.
I'm using microsoft's MVC platform to create a web application. In this application, users will fill out a HTML form, click submit, get redirected to a successful message page and receive a printout in pdf form of the form. In addition, I also want to save their entries in the form in a database. I'm using iTextSharp to create and edit the PDF.
I'm looking for advice on managing the workflow in MVC. Specifically, I have a controller method that would direct the workflow for :
saving the form entries to a database,
creating and editing the PDF,
directing the user to a success page,
returning the PDF for the user to download.
My major question is: how should I delegate the tasks of redirecting to the success page and returning the pdf? As of right now, my controller seems to be able to return only one or the other. Can I redirect to a View that "comes with" a PDF?
What I do in a case like this is create a controller action that returns the PDF associated with a given entry id in the database.
public FileResult DownloadPdf(int id)
{
byte[] data;
// ... Generate PDF
return File(data, "application/pdf");
}
Then the controller action which handles the form POST does the following:
Saving the entry values to the database.
Retrieving the primary key of the saved form entry set.
Redirecting to the success view and passing the primary key to it.
Then the success view can use the id it has been given to either link to the PDF (as a download link) or can embed it in an iframe (yuck!).
Note that this only works because you are saving the values in a database. If don't use a database, you have to either:
Encode all the form data in the query string (which has a limited size)
Store the form data in the Session (which is not recommended)
Hope that helps.
I need to be able to serve audio files to an mvc app while preventing direct access. Ideally the page would render with a player control so the user can start/stop the audio linked to the database record (audio files are in a folder not the db). I have a controller action like this:
Response.Clear();
Response.ContentType = "audio/wav";
Response.TransmitFile(audioFilename);
Response.End();
return Response;
and the view uses the RenderAction method
<% Html.RenderAction("ServeAudioFile"); %>
this works but it won't display inline on the existing view, it opens a new page with just the media control. Am I totally barking up the wrong tree or is there a way to embed the response in the existing view? works exactly as I would like but I can't control access to the file.
Your controller action looks very strange, one would say classic WebForms code, not ASP.NET MVC. Normally controller actions return ActionResult:
public ActionResult ServeAudioFile()
{
Response.AppendHeader("Content-Disposition", "inline; filename=music.wav");
return File(audioFilename, "audio/wav");
}
As far as embedding the player inline inside the browser you may take a look at the <object> tag and point the url to this controller action (don't use Html.RenderAction). And here's another example of using the object tag.
As far as preventing direct access is concerned this is not possible, the music file is played on the client and if you prevent access to it you won't be able to play it. It is the same as trying to hide the HTML of your site. This simply is something that shouldn't be done. On the other hand you could allow only authenticated users to access this controller action by decorating it with the [Authorize] attribute.
I have an ASP.Net MVC application. I would like to create a view that lists the contents of a simple collection and show it in a new browser window.
Is there a way to show a view in a new browser window using a simple link?
I have struck out with Html.ActionLink. The Url.Action below does result in the Controller action being called but does not open in a new browser window.
Open MVC View in New Browser.
Is opening a view in a new browser window possible in MVC?
If so, does anyone know how?
You can specify target=_blank in the HTML properties of your link to open it in a new window. There's a parameter on Html.ActionLink that allows you to specify arbitrary HTML properties to add to your link, like so:
Html.ActionLink("Text", "Action", new { id = 1 }, new { target = "_blank" });
I have an image gallery which has the following route:
// gallery id
routes.MapRoute(
"gallery-route",
"gallery/{galleryID}/{imageID}/{title}",
new { controller = "Gallery", action = "Index", galleryID = (string)null, imageID = (string) null, title = (string) null},
new { galleryID = #"\d+" }
);
I can have URLS like :
example.com/gallery/4/23 - shows
gallery 4 and image 23
example.com/gallery/4 - shows
gallery 4 and first image in that
gallery
I was trying to make an 'edit in place' mode which lets an administrator edit the images and running into several issues. Currently the editing functionality is non-AJAX.
1) How should i keep a 'sticky' edit mode parameter. There won't be an 'edit' button next to each image. i want the edit mode to be 'sticky', but then I'm finding I either need to set it in session or add a parameter to every single link on the page which is clumsy.
2) I have caching enabled for this view. Therefore if i make a change and refresh - the original cached view remains.
Can anyone give me any thoughts?
why not change the output on the view depending on the users authorisation status. Using inline code and Html helper functions in the ascx to either write out the values in HTML for readonly roles and for editor roles add a post form around input controls with the current values in. Then on the controller handle post in a separate procedure to save the edits.
or simply add an edit view ascx as well as a read view ascx.
Also when the post controller procedure fires replace the cache object with the new data recorded in the post.
finally of you have image caching problems when administering the gallery. Try adding a random string to the query eg:
function GetNewUrl(url)
{
Random rnd = new Random();
return url +"?"+rnd.Next(1000).ToString();
}
You need to flush the cache for the page when a change is made. Don't show the cached page or cache the page when the user is logged in as administrator, as they will have a different view with edit controls etc. For a sticky mode where an administrator can choose to be in edit mode throughout the site this would have to be stored in the session. I use something based on this for caching controller actions, with an additional method to determine whether to cache the output/use the cached output.