Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. using Microsoft.AspNetCore.Hosting;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using System.IO;
  5. using System.Net.Http;
  6. using System.Web;
  7. using System.Net.Http.Headers;
  8. using System.Diagnostics;
  9. using System;
  10. using System.Globalization;
  11. using Microsoft.Extensions.FileProviders;
  12. using Newtonsoft.Json;
  13.  
  14. namespace challenge.Web.Controllers.maps
  15. {
  16. [Produces("application/json")]
  17. [Route("api/[controller]")]
  18. public class MapsController : Controller
  19. {
  20. private IHostingEnvironment _hostingEnvironment;
  21.  
  22. public MapsController(IHostingEnvironment hostingEnvironment)
  23. {
  24. _hostingEnvironment = hostingEnvironment;
  25. }
  26. [HttpPost, DisableRequestSizeLimit]
  27. public ActionResult UploadFile()
  28. {
  29. try
  30. {
  31. // var obj = JsonConvert.SerializeObject(context.Request);
  32. var xml = Request.Form.Files["File"].ToString();
  33. var httpRequest = HttpContext.Request.Form;
  34. var postedFile = httpRequest.Files["File"];
  35. string outputFile = Request.Form["ProcessingMode"].ToString();
  36. var startDate = Request.Form["StartDate"];
  37. var file = httpRequest.Files[0];
  38. string fullPath = "";
  39. string folderName = "Upload";
  40.  
  41. string antFile = @"C:\Users\Monika\Downloads\Generatorius\Generator\apache-ant-1.7.0\bin\ant.bat";
  42. string build = @"C:\Users\Monika\Downloads\Generatorius\Generator\build.xml";
  43.  
  44.  
  45. string rootPath = @"C:\Users\Monika\Downloads\Generatorius\Generator";
  46. string newPath = Path.Combine(rootPath, folderName);
  47. if (!Directory.Exists(newPath))
  48. {
  49. Directory.CreateDirectory(newPath);
  50. }
  51. if (file.Length > 0)
  52. {
  53. string fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
  54. fullPath = Path.Combine(newPath, fileName);
  55. using (var stream = new FileStream(fullPath, FileMode.Create))
  56. {
  57. file.CopyTo(stream);
  58. }
  59. }
  60.  
  61.  
  62. string inputFile = fullPath;
  63. /*
  64. ProcessStartInfo procStartInfo5 = new ProcessStartInfo();
  65. procStartInfo5.FileName = "cmd.exe";
  66. procStartInfo5.Arguments = $"/c{antFile} -f={build} -DinputFile={inputFile} -DstartDate={startDate} -DxslFile=ProcessingDate -DoutputFile={outputFile}";
  67. Process.Start(procStartInfo5);
  68.  
  69. */
  70. //var fs = System.IO.File.OpenRead(inputFile);
  71. // var result = System.Text.Encoding.UTF8.GetBytes(@"C:\work\itvs\SYSANA\DP-5\XML_generatorius\Generator\Upload\aaaa.xml");
  72. // return File(result, "application/xml", "aaaa.xml");
  73. // GetChallenges();
  74. // return PhysicalFile(@"C:\Users\Monika\Downloads\Generatorius\Book1.xml", "application/xml", "Book1.xml");
  75.  
  76. //if (System.IO.File.Exists("C:\\Users\\Monika\\Downloads\\Generatorius\\Book1.xml"))
  77. // {// to know if the file is Exist or not
  78. // //Process File Here ...
  79. // }
  80. // else
  81. // {
  82. // return Json("NotFound");
  83. // }
  84. string contentType = "application/xml";
  85. HttpContext.Response.ContentType = contentType;
  86.  
  87. var result = new FileContentResult(System.IO.File.ReadAllBytes("C:\\Users\\Monika\\Downloads\\Book1.xml"), contentType)
  88. {
  89.  
  90. // FileDownloadName = $"{myFile.Title }" // + myFile.Extension
  91. FileDownloadName = "s.xml"
  92.  
  93. };
  94.  
  95. // System.IO.File.Delete (myFile.Path); //if you want to delete the file after download
  96.  
  97. return result;
  98. }
  99. catch (Exception err)
  100. {
  101. Console.WriteLine(err);
  102. }
  103. return BadRequest();
  104. }
  105.  
  106. //[HttpGet]
  107. //public IActionResult GetChallenges()
  108. //{
  109. // var physicalFilePath = "C:\\Users\\Monika\\Downloads\\Book1.xml";
  110. // var fileBytes = System.IO.File.ReadAllBytes(physicalFilePath);
  111.  
  112. // return this.File(fileBytes, "text/xml", "test.xml");
  113. // // return PhysicalFile(@"C:\Users\Monika\Downloads\Book1.xml", "application/xml", "Book1.xml");
  114. //}
  115.  
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement