How to download a file from the view in mvc? - c#

My requirement is to download a table that is present in the view in mvc
If I click a button, the table present in the view should be downloaded as excel file. In angularjs, I used option as ng-csv for this. But I don't know whether similar kind of option is present in mvc?
Could anyone help me with this ?

I'm not sure of what you mean by "a file that is present in the view".
If the user's file is generated server-side upon request, your button should link to an ActionMethod which returns a
FileContentResult
If you want the user's browser to convert the HTML page or a part of it to another format and save it, you should look into the html5 File API or a Javascript framework, not ASP.NET MVC.

Related

How to display .rst file content inside Razor page?

I have a web application (ASP.NET Mvc) and I need to display Python's package 'Readme' which comes from .rst file.
The problem is that, there are some built in fucntions that can display for example .md file content, but I can not find any solution to show this 'Readme' in .rst format inside my HTML. Ofcourse there are some online converters from .rst to .md that has API, but I would like to avoid that.
So, is there any mechanism provided in the framework that I am missing that can help me with that?

Post a serialized ASP.NET Model and a file with single Jquery Ajax call in ASP.NET MVC

I am developing a movie library application in asp.net. In admin page, I need to add a movie to the database(details such as name, actors, poster url). And also I need to upload the trailer of the movie to a specific folder in the server.
In my AddMovie View(which is strongly typed to a MovieDetails Model), I have a forms to include movie information as text and with a file input to upload a file(movie trailer). Within my controller I get the binded MovieDetails model and the uploaded file seperately as two parameters as follows.
At first I just posted from my view to this controller without using ajax. I was able to get the Model and the file to the controller to do what I needed to do.
Then I moved to ajax and tried to do this without page refresh.
Now no files cannot be uploaded. I tried to debug and noticed that uploadFile parameter of the controller is null although I attached a file to the file input in the view.
Is there any way to post the model to the controller and upload the file without completely changing the controller and using a single ajax call?
Or else is it possible pass the file path through a model attribute, access that file path and upload the relevant file in the controller, using Ajax.
Thank you!
As Darin mentioned, there is no simple way to handle file-uploading matters via ajax for some security reasons and you can't upload files to your server easily; but good news is there are some tricks which can resolve it, for instance, you can use jquery AjaxForm in your mvc form easily with a bit little modification. Take a look at jquery-ajax-form and its samples, you will get some good idea around it.
Normally you cannot upload files using AJAX. Modern browsers that support FormData would allow you to directly make AJAX requests containing multipart/form-data content. Also look at the native XMLHttpRequest object.
But if you need to support legacy browsers you will need some fallback mechanism such as hidden iframes or Flash movies. Plugins such as blueimp would detect the browser capabilities and upload the file using the best option that the browser supports.

Send MVC actionresult to printer

I have a controller with an action:
SomeController/ActionToBePrinted
ActionToBePrinted() returns an html view.
This action is called from a normal mvc razor view when pressing a button - how would I go about sending the content of the view to a printer when the button is pressed?
Aloha,
Hugo
You cant send direct to the printer.
I suggest you to create a custom ActionResult, that returns a PDF file or something like that. ASP.NET MVC Action Results and PDF Content
You can show a html page as well and open the print dialog using javascript like this
Click to Print This Page
But always the user has to start the print process, you cant do this programmatically.
You can perform a GET request (e.g. use window.open() and pass in URL or use AJAX) and put the returned HTML contents into a new window. Then use
Window.print(). Then simply close the window when you are done.
You could tie this directly into a single view by adding something in the body, but I prefer to use JavaScript in these cases. This keeps the design acting as a re-useable object or service that can be used across multiple views. In other words, you setup the controller-model, but no view. Instead, JavaScript steps in as the View.
Keep in mind that HTML is not a print format. So if you need to control the layout, you should be using a print technology such as PDF. XSLT provides an excellent means to create both HTML and PDF output using the same data, albeit it's a lot more work to create XSLT templates than it is to slap down window.print
Personally, I have an MVC page acting as a service that takes URL parameters. The page hooks into Adobe XSL-FO and uses the params to drive the output.

How to get this file prompted to the user?

Hi Im working on a MVC3 Razor project and I have been stuck on this problem for a few hours now...
I'm trying to get a html to pdf converter to serve a document to the user...
What I want is the following..
A page is rendered and displayed. On the bottom of the page there should be a little icon display something like download as pdf. and what that does is where my problem lies..
All the data that I want is dynamically created within a $("#content").html();
So what I have tried is a jquery/ajax function passing $("#content").html(); as a paramater to my function which creates the pdf (works but I have no clue how to prompt the created file to the user?)
Other solution was #(Html.ActionLink()) but I dont know how to pass the data ($("#content").html()) within that link?
And when trying to work with the functionallity to use the converter to go to the url was a dead end cuz it got its own session and got redirected to the loginpage..
Any help would be appriciated!
I am not familiar with JavaScript, but what if you save the PDF in the temp file first by calling you web service method, and then after it completed you can use JavaScript to navigate to the URL where generated PDF will be returned as content.
Since the XmlHTTPRequest Object cannot handle datatypes other than html, text, json, jsonp and xml you will need to redirect to the pdf location.
I'm not sure what exactly you're doing in your Ajax Request, but once that is completed you can just redirect the window(Form-Action) to the location of the created PDF. This wouldn't actually redirect the browser but only prompt for saving the file.
Why just create a function that returns the created PDF (during ajax request) as File result and then set the window's location to point to this action once the ajax request is completed successfully?
Edit: That means you are not saving the PDF anywhere. So the workaround, is to either use this jquery download plugin or append an iframe dynamically and then post the data through it. Hope this helps.
Figured out a workaround, what I did was write an extension to my HtmlHelper and use that to render my control into a html string instead of a view. Therefor I could use a actionlink to say render this page and get all the html data that way.
http://msug.vn.ua/Posts/Details/3301
Thanks for help tho!

How to Return A File and a strongly Typed data at the same time?

I am using asp.net mvc 1.0 and I want to return a XML file but I also want to return a strongly typed data back so I can update some fields.
Like the XML file will contain users who failed to be inserted into the database. So I want that to appear as a dialog save box what asp.net mvc return file() would do.
However I also want to return on the page like values like how many users failed to be added, how many users where added, etc.
So I want to use scafolding with the class file I want to pass it along. If this was a view I could pass it along as an object model but I don't see a parameter for that in File().
I also don't want to save the xml file onto the harddrive I want to do it through memory. So have a link that would display on the page to download the file and show the the data I want would not be desired.
I could be wrong, but I think you'll need to use JavaScript for this. What your're trying to do is a limitation of HTTP and from my understanding of HTTP, you can only return one file type per response since the protocol is a request->response.
You can have MVC return the strongly typed view in addition to the XML file name inside the ViewData. Then have a JavaScript function change the window.location property to the file's URL (or make a new window).
I'm not sure about the exact details on how to gracefully have the JavaScript spit the file out like alot of download websites have it.
Edit:
I found how to gracefully automate the download process, check this question out:
JavaScript automatic download of a file

Categories