using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; using System.Web; using System.Web.Http; using iBemsDataService.Model; using iBemsDataService.Util; namespace iBemsDataService.Controllers { public class UploadController : ApiController { private iBemsEntities db = new iBemsEntities(); public async Task Post() { // Check whether the POST operation is MultiPart? if ( !Request.Content.IsMimeMultipartContent() ) { throw new HttpResponseException( HttpStatusCode.UnsupportedMediaType ); } Uri uri = Request.RequestUri; var query = HttpUtility.ParseQueryString( uri.Query ); string siteId = query.Get( "SiteId" ); string categoryId = query.Get( "FileCategoryId" ); var fileInfo = new CmFile { SiteId = int.Parse( siteId ), FileCategoryId = int.Parse( categoryId ) }; // Prepare CustomMultipartFormDataStreamProvider in which our multipart form // data will be loaded. string fileSaveLocation = HttpContext.Current.Server.MapPath( "~/App_Data" ); var provider = new CustomMultipartFormDataStreamProvider( db, fileSaveLocation, int.Parse( siteId ), int.Parse( categoryId ) ); var files = new List(); try { // Read all contents of multipart message into CustomMultipartFormDataStreamProvider. await Request.Content.ReadAsMultipartAsync( provider ); //foreach ( MultipartFileData file in provider.FileData ) //{ // var info = new FileInfo( file.LocalFileName ); // provider._file.FileSize = (int)info.Length; // files.Add( info.GetType().ToString() ); // files.Add( Path.GetFileName( file.LocalFileName ) ); //} provider.Success(); // Send OK Response along with saved file names to the client. return Request.CreateResponse( HttpStatusCode.OK , provider._file ); } catch ( System.Exception e ) { return Request.CreateErrorResponse( HttpStatusCode.InternalServerError , e ); } } // We implement MultipartFormDataStreamProvider to override the filename of File which // will be stored on server, or else the default name will be of the format like Body- // Part_{GUID}. In the following implementation we simply get the FileName from // ContentDisposition Header of the Request Body. public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider, IDisposable { public CmFile _file = new CmFile(); private iBemsEntities _db; public CustomMultipartFormDataStreamProvider( iBemsEntities db, string path, int siteId, int categoryId ) : base( path ) { _db = db; _file.SiteId = siteId; _file.FileCategoryId = categoryId; } public override string GetLocalFileName( HttpContentHeaders headers ) { var name = !string.IsNullOrWhiteSpace( headers.ContentDisposition.FileName ) ? headers.ContentDisposition.FileName : "NoName"; _file.Name = name.Replace( "\"" , string.Empty ); return Path.GetTempFileName(); } public void Success() { var file = FileData[0]; _file.ContentType = MimeMapping.GetMimeMapping( _file.Name ); var info = new FileInfo( file.LocalFileName ); _file.FileSize = (int)info.Length; _file.CreatedDate = DateTime.Now; try { _db.CmFile.Add( _file ); _db.SaveChanges(); } catch( Exception ex ) { throw ex; } try { var m = FileCategoryManager.GetInstance(); File.Move( file.LocalFileName , m.GetFilePath( _file ) ); } catch( Exception ex ) { try { _db.CmFile.Remove( _file ); _db.SaveChanges(); } catch( Exception eex ) { throw eex; } throw ex; } } public void Dispose() { foreach ( var file in FileData ) { File.Delete( file.LocalFileName ); } } } /* [ActionName("CheckSameEquipCodeType")] public int PostCheckSameEquipCodeType(FmsEquipmentCodeType param) { IEnumerable newKeyData = (from data in db.FmsEquipmentCodeType where data.SiteId == param.SiteId && data.Name == param.Name select data).Take(1); if (newKeyData.Count() > 0) { //return BadRequest("중복된 이름이나 별명이 이미 존재 합니다!"); return 1; } return 0; } [ActionName("GetMaxPropertyId")] public int PostGetMaxPropertyId(GetMaxPropertyId_Param param) { IEnumerable newKeyData = (from data in db.BemsMonitoringPoint where data.SiteId == param.SiteId && data.FacilityCode == param.FacilityCode orderby data.PropertyId descending select data).Take(1); if (newKeyData == null) return 1; if (newKeyData.Count() == 0) { return 1; } BemsMonitoringPoint tf = newKeyData.First(); return tf.PropertyId + 1; }*/ } public class FileCategory { public int SiteId { get; set; } public int FileCategoryId { get; set; } } public class FileInformation { public int SiteId { get; set; } public int FileCategoryId { get; set; } public int FileId { get; set; } public string FileName { get; set; } public Nullable FileSize { get; set; } public Nullable CreatedDate { get; set; } public string ContentType { get; set; } } }