PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/IntelWebSite/wfIntelWebImport.aspx.cs

https://github.com/sirivedula/IntelWebPro
C# | 131 lines | 124 code | 7 blank | 0 comment | 8 complexity | 5c066a30cc88bfa1a9cd174773f7ac39 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.IO;
  8. using IntelWeb;
  9. using System.Text;
  10. namespace IntelWebSite
  11. {
  12. public partial class wfIntelWebImport : System.Web.UI.Page
  13. {
  14. protected void Page_Load(object sender, EventArgs e)
  15. {
  16. this.Master.UseMultiPartForm = true;
  17. }
  18. private CurrentUser cuser;
  19. protected void Page_LoadComplete(object sender, EventArgs e)
  20. {
  21. string SaveFile="";
  22. cuser = new CurrentUser();
  23. cuser.userName = "Balaji";
  24. cuser.Load();
  25. lkup_tier tier = new lkup_tier(cuser);
  26. List<IntelWebObject> Tiers = tier.Load();
  27. this.ltrTiers.Text = string.Join("", Tiers.Cast<lkup_tier>().Select(x => "<option value=\"" + HttpUtility.HtmlEncode(x.tier_code) + "\">" + HttpUtility.HtmlEncode(x.tier_code) + "</option>").ToArray());
  28. if (IsPostBack)
  29. {
  30. bool createJob = false;
  31. if (Request.Files.Count > 0)
  32. {
  33. HttpPostedFile File1 = Request.Files[0];
  34. if (File1.ContentLength > 0)
  35. {
  36. SaveFile = System.IO.Path.GetFileName(File1.FileName);
  37. if (System.IO.Path.GetExtension(SaveFile).Equals(".xls", StringComparison.InvariantCultureIgnoreCase))
  38. {
  39. string SaveLocation = Server.MapPath("Data") + "\\" + SaveFile;
  40. try
  41. {
  42. File1.SaveAs(SaveLocation);
  43. createJob = true;
  44. }
  45. catch (Exception ex)
  46. {
  47. ltrMessage.Text = "<div style=\"border:1px solid #FF0000;\">Error: " + HttpUtility.HtmlEncode(ex.Message) + "</div>";
  48. }
  49. }
  50. else
  51. {
  52. ltrMessage.Text = "<div style=\"border:1px solid #FFFF00;\">Impoter support's only Excel files.</div>";
  53. }
  54. }
  55. else
  56. {
  57. string xlsText = Request.Form["xlsTextArea"];
  58. if (!string.IsNullOrEmpty(xlsText))
  59. {
  60. SaveFile = randomFile() + ".txt";
  61. StreamWriter outfile = new StreamWriter(Server.MapPath("Data") + "\\" + SaveFile);
  62. outfile.Write(xlsText);
  63. outfile.Close();
  64. createJob = true;
  65. }
  66. }
  67. }
  68. if (createJob)
  69. {
  70. JobQ job = new JobQ(cuser);
  71. job.jobType = "Import";
  72. job.jobStatus = "active";
  73. job.tiername = Request.Form["tiername"];
  74. job.filename = SaveFile;
  75. if (job.save())
  76. {
  77. ltrMessage.Text = "<div style=\"border:1px solid #00FF00;\">The file has been uploaded.</div>";
  78. }
  79. else
  80. {
  81. ltrMessage.Text = "<div style=\"border:1px solid #FF0000;\">Error: " + HttpUtility.HtmlEncode(job.saveErrorText) + "</div>";
  82. }
  83. }
  84. }
  85. ltrJobStatus.Text = JobStatusHTML();
  86. }
  87. private string JobStatusHTML()
  88. {
  89. StringBuilder sb = new StringBuilder();
  90. JobQ jobs = new JobQ(cuser);
  91. jobs.selectFields = "jobtype,jobstatus,jobid,queuedtime,starttime,endtime,filename,queueduser,tiername";
  92. List<IntelWebObject> jobList = (List<IntelWebObject>) jobs.Load("", "queuedtime desc");
  93. sb.Append("<div class=\"centered\"><table class=\"webgrid\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><thead class=\"gridheader\"><tr><th>Job Id</th><th>Job Type</th><th>Job Status</th><th>Queued Time</th><th>Start Time</th><th>End Time</th><th>File Name</th><th>Queued User</th><th>Tier Name</th><th>Results</th></tr></thead>");
  94. int rowCnt = 0;
  95. foreach (JobQ job in jobList)
  96. {
  97. sb.Append("<tr " + ((rowCnt % 2 == 0)?"":"class=\"gridodd\"") + "><td>" + HttpUtility.HtmlEncode(job.jobId.ToString()) + "</td>");
  98. sb.Append("<td>" + HttpUtility.HtmlEncode(job.jobType) + "</td>");
  99. sb.Append("<td>" + HttpUtility.HtmlEncode(job.jobStatus) + "</td>");
  100. sb.Append("<td>" + HttpUtility.HtmlEncode(job.queuedtime.ToString("MM/dd/yyyy hh:mm")) + "</td>");
  101. sb.Append("<td>" + HttpUtility.HtmlEncode((job.starttime.HasValue ? job.starttime.Value.ToString("MM/dd/yyyy hh:mm"):"")) + "</td>");
  102. sb.Append("<td>" + HttpUtility.HtmlEncode(job.endtime.HasValue ? job.endtime.Value.ToString("MM/dd/yyyy hh:mm"):"") + "</td>");
  103. sb.Append("<td>" + HttpUtility.HtmlEncode(job.filename) + "</td>");
  104. sb.Append("<td>" + HttpUtility.HtmlEncode(job.queueduser) + "</td>");
  105. sb.Append("<td>" + HttpUtility.HtmlEncode(job.tiername) + "</td>");
  106. sb.Append("<td>" + (job.jobStatus.Equals("done", StringComparison.InvariantCultureIgnoreCase)?"<a href=\"javascript:void(0);\" onclick=\"showResults();\">Download</a>":"") + "</tr>");
  107. rowCnt++;
  108. }
  109. sb.Append("</table></div>");
  110. return sb.ToString();
  111. }
  112. private string randomFile()
  113. {
  114. string result = "";
  115. string randomStr = "abcdefghijklmnopqrstuvwxyz0123456789";
  116. Random rnd = new Random();
  117. for (int i = 0; i < 10; i++)
  118. {
  119. result += randomStr[rnd.Next(randomStr.Length - 1)];
  120. }
  121. return result;
  122. }
  123. }
  124. }