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()
- {
-
- 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 )
- };
-
-
- 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
- {
-
- await Request.Content.ReadAsMultipartAsync( provider );
-
-
-
-
-
-
-
-
- provider.Success();
-
- return Request.CreateResponse( HttpStatusCode.OK , provider._file );
- }
- catch ( System.Exception e )
- {
- return Request.CreateErrorResponse( HttpStatusCode.InternalServerError , e );
- }
- }
-
-
-
-
- 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 );
- }
- }
- }
-
- }
- 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; }
- }
- }
|