The article previous I was introduce about Using Microsoft Report Viewer in ASP.NET MVC3. In this article I will introduce about how to save .pdf file to database in ASP.NET MVC 3.
1. Create a new database to store pdf file and set named Database.mdf.
2. Create a new table and set named DataFiles
3. Create a new DataFileModel.edmx under Models folder.
4.Add two buttons Save and Download in Index.aspx file
</pre> <div style="float: left; width: 100%;"><% using(Html.BeginForm("SaveFile","Home")) { %> <input style="float: left;" type="submit" value="Save file" /> <%} %> <% using(Html.BeginForm("Download","Home")) { %> <input style="float: left;" type="submit" value="Download" /> <%} %></div> <pre>
5. Create method SaveFile in HomeController.
public ActionResult SaveFile() { LocalReport localReport = new LocalReport(); localReport.ReportPath = Server.MapPath("~/Reports/rptCustomer.rdlc"); ReportDataSource reportDataSource = new ReportDataSource("Customer", new CustomerModel().GetAllCustomer()); localReport.DataSources.Add(reportDataSource); string reportType = "pdf"; string mimeType; string encoding; string fileNameExtension; //The DeviceInfo settings should be changed based on the reportType //http://msdn2.microsoft.com/en-us/library/ms155397.aspx string deviceInfo = "" + " PDF" + " 8.5in" + " 11in" + " 0.5in" + " 1in" + " 1in" + " 0.5in" + ""; Warning[] warnings; string[] streams; byte[] renderedBytes; //Render the report renderedBytes = localReport.Render( reportType, deviceInfo, out mimeType, out encoding, out fileNameExtension, out streams, out warnings); // Save to Database DataFile dataFile = new DataFile { FileName = "Customer", FileData = renderedBytes, FileExtension = ".pdf" }; bool Status; try { context.DataFiles.AddObject(dataFile); context.SaveChanges(); ViewBag.Status = true; return View(); } catch { ViewBag.Status = false; return View(); } }
6. Create SaveFile.aspx to display message after save file
</pre> <h2></h2> <pre> <% if (ViewBag.Status == true) { %></pre> <div>Save file to Database is successful!</div> <pre> <% } else { %></pre> <div>Save file to Database is fail!</div> <pre> <% } %>
7. Build and run website so click Save button to save file to database.
8. Open database to check data.
9. The next we will implement action download pdf file from database.
Create a new method Download in HomeController.
public ActionResult Download() { DataFile dataFile = context.DataFiles.SingleOrDefault(p => p.Id == 1); byte[] fileData = dataFile.FileData; // Generate PDF file Warning[] warnings = null; String[] streamids = null; String mimeType = null; String encoding = null; String extension = null; Response.Clear(); Response.ClearHeaders(); Response.ClearContent(); Response.ContentType = mimeType; Response.AddHeader("Content-Disposition", string.Format("attachment; filename=Customer.pdf")); Response.BinaryWrite(fileData); Response.End(); return RedirectToAction("Index"); }
10. Build and run website, so click Download button to download pdf file.
View pdf file after download.
Happy coding!
how should i do it using ado.net
Pingback: Medicare Card » aspx Search Database
Pingback: Medicare Code PDF » aspx Search Database