UploadController.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Http;
  7. using System.Net.Http.Headers;
  8. using System.Threading.Tasks;
  9. using System.Web;
  10. using System.Web.Http;
  11. using iBemsDataService.Model;
  12. using iBemsDataService.Util;
  13. namespace iBemsDataService.Controllers
  14. {
  15. public class UploadController : ApiController
  16. {
  17. private iBemsEntities db = new iBemsEntities();
  18. public async Task<HttpResponseMessage> Post()
  19. {
  20. // Check whether the POST operation is MultiPart?
  21. if ( !Request.Content.IsMimeMultipartContent() )
  22. {
  23. throw new HttpResponseException( HttpStatusCode.UnsupportedMediaType );
  24. }
  25. Uri uri = Request.RequestUri;
  26. var query = HttpUtility.ParseQueryString( uri.Query );
  27. string siteId = query.Get( "SiteId" );
  28. string categoryId = query.Get( "FileCategoryId" );
  29. var fileInfo = new CmFile
  30. {
  31. SiteId = int.Parse( siteId ),
  32. FileCategoryId = int.Parse( categoryId )
  33. };
  34. // Prepare CustomMultipartFormDataStreamProvider in which our multipart form
  35. // data will be loaded.
  36. string fileSaveLocation = HttpContext.Current.Server.MapPath( "~/App_Data" );
  37. var provider = new CustomMultipartFormDataStreamProvider( db, fileSaveLocation, int.Parse( siteId ), int.Parse( categoryId ) );
  38. var files = new List<string>();
  39. try
  40. {
  41. // Read all contents of multipart message into CustomMultipartFormDataStreamProvider.
  42. await Request.Content.ReadAsMultipartAsync( provider );
  43. //foreach ( MultipartFileData file in provider.FileData )
  44. //{
  45. // var info = new FileInfo( file.LocalFileName );
  46. // provider._file.FileSize = (int)info.Length;
  47. // files.Add( info.GetType().ToString() );
  48. // files.Add( Path.GetFileName( file.LocalFileName ) );
  49. //}
  50. provider.Success();
  51. // Send OK Response along with saved file names to the client.
  52. return Request.CreateResponse( HttpStatusCode.OK , provider._file );
  53. }
  54. catch ( System.Exception e )
  55. {
  56. return Request.CreateErrorResponse( HttpStatusCode.InternalServerError , e );
  57. }
  58. }
  59. // We implement MultipartFormDataStreamProvider to override the filename of File which
  60. // will be stored on server, or else the default name will be of the format like Body-
  61. // Part_{GUID}. In the following implementation we simply get the FileName from
  62. // ContentDisposition Header of the Request Body.
  63. public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider, IDisposable
  64. {
  65. public CmFile _file = new CmFile();
  66. private iBemsEntities _db;
  67. public CustomMultipartFormDataStreamProvider( iBemsEntities db, string path, int siteId, int categoryId ) : base( path )
  68. {
  69. _db = db;
  70. _file.SiteId = siteId;
  71. _file.FileCategoryId = categoryId;
  72. }
  73. public override string GetLocalFileName( HttpContentHeaders headers )
  74. {
  75. var name = !string.IsNullOrWhiteSpace( headers.ContentDisposition.FileName ) ? headers.ContentDisposition.FileName : "NoName";
  76. _file.Name = name.Replace( "\"" , string.Empty );
  77. return Path.GetTempFileName();
  78. }
  79. public void Success()
  80. {
  81. var file = FileData[0];
  82. _file.ContentType = MimeMapping.GetMimeMapping( _file.Name );
  83. var info = new FileInfo( file.LocalFileName );
  84. _file.FileSize = (int)info.Length;
  85. _file.CreatedDate = DateTime.Now;
  86. try
  87. {
  88. _db.CmFile.Add( _file );
  89. _db.SaveChanges();
  90. }
  91. catch( Exception ex )
  92. {
  93. throw ex;
  94. }
  95. try
  96. {
  97. var m = FileCategoryManager.GetInstance();
  98. File.Move( file.LocalFileName , m.GetFilePath( _file ) );
  99. }
  100. catch( Exception ex )
  101. {
  102. try
  103. {
  104. _db.CmFile.Remove( _file );
  105. _db.SaveChanges();
  106. }
  107. catch( Exception eex )
  108. {
  109. throw eex;
  110. }
  111. throw ex;
  112. }
  113. }
  114. public void Dispose()
  115. {
  116. foreach ( var file in FileData )
  117. {
  118. File.Delete( file.LocalFileName );
  119. }
  120. }
  121. }
  122. /*
  123. [ActionName("CheckSameEquipCodeType")]
  124. public int PostCheckSameEquipCodeType(FmsEquipmentCodeType param)
  125. {
  126. IEnumerable<FmsEquipmentCodeType> newKeyData = (from data in db.FmsEquipmentCodeType
  127. where data.SiteId == param.SiteId && data.Name == param.Name
  128. select data).Take(1);
  129. if (newKeyData.Count() > 0)
  130. {
  131. //return BadRequest("중복된 이름이나 별명이 이미 존재 합니다!");
  132. return 1;
  133. }
  134. return 0;
  135. }
  136. [ActionName("GetMaxPropertyId")]
  137. public int PostGetMaxPropertyId(GetMaxPropertyId_Param param)
  138. {
  139. IEnumerable<BemsMonitoringPoint> newKeyData = (from data in db.BemsMonitoringPoint
  140. where data.SiteId == param.SiteId && data.FacilityCode == param.FacilityCode
  141. orderby data.PropertyId descending
  142. select data).Take(1);
  143. if (newKeyData == null) return 1;
  144. if (newKeyData.Count() == 0)
  145. {
  146. return 1;
  147. }
  148. BemsMonitoringPoint tf = newKeyData.First();
  149. return tf.PropertyId + 1;
  150. }*/
  151. }
  152. public class FileCategory
  153. {
  154. public int SiteId { get; set; }
  155. public int FileCategoryId { get; set; }
  156. }
  157. public class FileInformation
  158. {
  159. public int SiteId { get; set; }
  160. public int FileCategoryId { get; set; }
  161. public int FileId { get; set; }
  162. public string FileName { get; set; }
  163. public Nullable<int> FileSize { get; set; }
  164. public Nullable<System.DateTime> CreatedDate { get; set; }
  165. public string ContentType { get; set; }
  166. }
  167. }