Generate doodle report on post - asp.net-mvc

Lately I've been using doodle reporting to generate excel and pdf reports. I was doing that with a button link because the report is not dynamic.
But now I have a requirement where the parameters are dynamic, where I have a form of parameters and the users will fill these form and hit submit. The form will submit to another action and that action should generate a pdf report.
When I tried this code:
return new ReportResult(report, new PdfReportWriter());
It just generate the report in the page and I'm unable to download. Any idea how?
I've already included all the required dlls and I'm able to generate when I'm using an actionLink.

To solve the problem specify the content type and filename:
return new ReportResult(report, new ExcelReportWriter(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { FileName = "Report.xls" };

Related

Rails - How to assign temporary id of a post to reference images

I have a simple blog site built on rails. I have a Post model and Picture model.
A Post has_many pictures and
A Picture belongs_to post
Below is what I want to achieve -
When I start to create new post, I will provide a link in the form that opens up a new window where I can upload multiple reference images with some name field as well.
Then I will use a macro string in my post body like ##IMG_<image-name>##.
I will dynamically create a new <img> tag to display relevant images, while the user is still creating the post.
My Problems -
As you know, during the time of post creation, I will not have a post_id.
Then how do I relate the reference_images being uploaded to a particular post?
Can I somehow assign a temporary-id to the post as soon as I enter create page and then after I save the post, anyway rails will takeover?
Hope you got where I am stuck. Please help!
Thanks in advance.
I appreciate all the responses you have given. But I had to achieve this is another hacky way for my needs -
HTML
I provided a <input type="file" id="refImg" onchange="javascript: uploadImage(this);">.
After the user has selected the required image, the uploadImage() funtion would create a dynamic form and do an AJAX POST to a distinct API. http://example.com/uploadImage
Rails
In rails controller referenceImages#uploadImage, I used paperclip to upload the image to cloudinary. The cloudinary upload call will return back with the image URL, if the upload is successful.
I responded with this URL in the JSON format.
JS
In javascript function uploadImage(), in the success function of ajax call, I parsed the URL from the response and dynamically created a <img src="parsed_image_url"> tag and appended it to the current cursor position of post body.

Handling routes in asp.net mvc

I have an action that I want to use to handle the saving of a report for a project and a sub project. The code to run is essentially the same so I thought I could create a custom save action and then specify the routes; however, it doesn't seem to be working as expected.
My action signature is
[Route("Project/{projectNumber}/Reports/ProjectHoursAndCosts/save")]
[Route("Project/{projectNumber}/Reports/ProjectHoursAndCosts/{subProjectNumber}/save")]
public ActionResult ProjectHoursAndCosts(ProjectHoursAndCostsViewModel projectHoursAndCostsViewModel)
If I remove one of the routes it works as expected. Any ideas?
I've set the action in form
(Html.BeginForm("Save", "Reports"))
If my initial url is
http://somehost/somewebapp/Project/61033651/Reports/ProjectHoursAndCosts
it seems to work ok, however if I try to save a sub project which has the url
http://somehost/somewebapp/Project/61033651/Reports/ProjectHoursAndCosts/61033651-1
When I hit save the url changes back to
http://somehost/somewebapp/Project/61033651/Reports/ProjectHoursAndCosts/save
I would expect it to submit to the url
http://somehost/somewebapp/Project/61033651/Reports/ProjectHoursAndCosts/61033651-1/save
Update
Looking at the raw form element in the html page I can see that even though the url is http://somehost/somewebapp/Project/61033651/Reports/ProjectHoursAndCosts/61033651-1, the actual post url is still somewebapp/Project/61033651/Reports/ProjectHoursAndCosts/save
How can I change the action attribute of the form to use the correct url?

Rails scraping - submitting a form

I am filling in a form on a page and submitting it.
This should trigger the download of a file.
However, when I try to save the output of the download, I get the source code of the page rather than the file.
My code is:
mechanize = Mechanize.new
mechanize.pluggable_parser.default = Mechanize::Download
page = mechanize.get('http://page.com/')
form = page.forms.first
form.radiobuttons_with(name: 'presence')[0].check
form.source = "btce"
form.label = "BTC/USD"
mechanize.get_file(form.submit).save!('page.csv')
How can I save a file which is downloaded when I submit a form?
Does the file automatically begin downloading once you submit the form?
Submitting a form may return a new page, also new scripts/stylesheets can be loaded. Which possibly explains why your file contains the source code since that's what you're downloading. (Mechanize doesn't throw an error if you download a web page)
For example, I use Mechanize to fill out Google's search form and submit it and save the results to google_search.csv. The new file contains a mixture of the page's source code along with javascript, mySQL, and its stylesheets.
You can dig through the page's source code using Firebug and pinpoint what exactly happens when you submit the form, which can likely be a link that's invoked but you were unaware of.

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/

how to allow a user to upload a spreadsheet in asp.net mvc

i want a user to have file picker and then choose a spreadsheet which will then be parsed by a controller action. are there any examples of how to do this?
This article shows how to add a file upload control on an asp.net mvc page:
http://www.hanselman.com/blog/ABackToBasicsCaseStudyImplementingHTTPFileUploadWithASPNETMVCIncludingTestsAndMocks.aspx
With this line, you get the file from request:
HttpPostedFile hpf = Request.Files[file] as HttpPostedFile;
After getting it, you can do whatever you want with it.

Resources