Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. [HttpPost]
  2. public ActionResult ShowExcelFile(HttpPostedFileBase getFile)
  3. {
  4. //Make something with values of getFile
  5. return PartialView("ShowExcelFile");
  6. }
  7.  
  8. @using (Html.BeginForm("ShowExcelFile", "ShowExcel", FormMethod.Post, new { enctype = "multipart/form-data" }))
  9. {
  10. <input type="file" id="getFile" name="getFile" /><br />
  11. <input type="submit" value="Upload file" />
  12. }
  13.  
  14. using Excel;
  15.  
  16. [HttpPost]
  17. public ActionResult ShowExcelFile(HttpPostedFileBase getFile)
  18. {
  19. if (getFile != null && getFile.ContentLength > 0)
  20. {
  21. // .xlsx
  22. IExcelDataReader reader = ExcelReaderFactory.CreateOpenXmlReader(getFile.InputStream);
  23.  
  24. // .xls
  25. IExcelDataReader reader = ExcelReaderFactory.CreateBinaryReader(getFile.InputStream);
  26.  
  27. reader.IsFirstRowAsColumnNames = true; // if your first row contains column names
  28. }
  29.  
  30. return PartialView("ShowExcelFile");
  31. }
  32.  
  33. DataSet dataSet = reader.AsDataSet();
  34.  
  35. @using (Html.BeginForm("ShowExcelFile", "ShowExcel", FormMethod.Post, new { enctype = "multipart/form-data" }))
  36. {
  37. @Html.TextBoxFor(m => m.Files, new { type = "file", name = "Files" })<br />
  38. <input type="submit" value="Upload file" />
  39. }
  40.  
  41. public class AModel
  42. {
  43. public AModel()
  44. {
  45. Files = new List<HttpPostedFileBase>();
  46. }
  47.  
  48. public List<HttpPostedFileBase> Files { get; set; }
  49. // Rest of model details
  50. }
  51.  
  52. [HttpPost]
  53. public ActionResult ShowExcelFile(AModel model)
  54. {
  55. var file = model.Files[0];
  56. ...
  57. }
  58.  
  59. string fileExtension = Path.GetExtension(getFile.FileName);
  60.  
  61. //save this file using
  62.  
  63.  
  64.  
  65. string path = Path.Combine(Server.MapPath(Url.Content("~/Content")), "Name"+fileExtension);
  66. file.SaveAs(path);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement