123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- 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<HttpResponseMessage> 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<string>();
- 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<FmsEquipmentCodeType> 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<BemsMonitoringPoint> 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<int> FileSize { get; set; }
- public Nullable<System.DateTime> CreatedDate { get; set; }
- public string ContentType { get; set; }
- }
- }
|