public Task <HttpResponseMessage> Post(string DO, string CreatedBy) { try { var fileuploadPath = HttpContext.Current.Server.MapPath("~/App_Data/Files/"); var httpContext = HttpContext.Current; HttpRequestMessage request = this.Request; if (!request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.UnsupportedMediaType)); } string root = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Files"); MultipartFormDataStreamProvider provider = new MultipartFormDataStreamProvider(root); var task = request.Content.ReadAsMultipartAsync(provider). ContinueWith(o => { foreach (var file in provider.FileData) { FileInfo finfo = new FileInfo(file.LocalFileName); DocumentBO documentBO = new DocumentBO { Dockey = Guid.NewGuid(), DocType = (DocType)Enum.Parse(typeof(DocType), finfo.Extension), CreatedBy = new UserInfoRepository().GetbyField(CreatedBy).userkey, FileSizeInMB = (int)finfo.Length / 1024, FileType = string.Empty, //not sure name = finfo.Name }; OrderHeaderDocumentBO orderBO = new OrderHeaderDocumentBO { Document = documentBO, Orderkey = Guid.Parse(DO) }; //renaming the random file to Original file name string uploadingFileName = provider.FileData.Select(x => x.LocalFileName).FirstOrDefault(); string originalFileName = String.Concat(fileuploadPath, "\\" + (provider.Contents[0].Headers.ContentDisposition.FileName).Trim(new Char[] { '"' })); if (File.Exists(originalFileName)) { File.Delete(originalFileName); } File.Move(finfo.FullName, Path.Combine(root, DO, file.Headers.ContentDisposition.FileName.Replace("\"", ""))); DocumentDL dl = new DocumentDL(); dl.InsertDOHeaderDocument(orderBO); } return(new HttpResponseMessage() { Content = new StringContent("File uploaded.") }); } ); return(task); } catch { return(null); } }
public async Task <HttpResponseMessage> Upload() { try { var fileuploadPath = HttpContext.Current.Server.MapPath("~/App_Data/Files/"); var provider = new MultipartFormDataStreamProvider(fileuploadPath); var content = new StreamContent(HttpContext.Current.Request.GetBufferlessInputStream(true)); foreach (var header in Request.Content.Headers) { content.Headers.TryAddWithoutValidation(header.Key, header.Value); } await content.ReadAsMultipartAsync(provider); string DO = provider.FormData[0]; string CreatedBy = provider.FormData[1]; ////renaming the random file to Original file name //string uploadingFileName = provider.FileData.Select(x => x.LocalFileName).FirstOrDefault(); //string originalFileName = String.Concat(fileuploadPath, "\\" + (provider.Contents[0].Headers.ContentDisposition.FileName).Trim(new Char[] { '"' })); //if (File.Exists(originalFileName)) //{ // File.Delete(originalFileName); //} //File.Move(uploadingFileName, originalFileName); foreach (var file in provider.FileData) { //renaming the random file to Original file name string uploadingFile = file.LocalFileName; string originalFile = String.Concat(fileuploadPath, DO + "\\" + (file.Headers.ContentDisposition.FileName).Trim(new Char[] { '"' })); string strFileName = file.Headers.ContentDisposition.FileName.Replace('"', ' ').Trim(); if (File.Exists(originalFile)) { File.Delete(originalFile); } if (!Directory.Exists(String.Concat(fileuploadPath, DO))) { Directory.CreateDirectory(String.Concat(fileuploadPath, DO)); } File.Move(uploadingFile, originalFile); FileInfo finfo = new FileInfo(originalFile); DocumentBO documentBO = new DocumentBO() { Dockey = Guid.NewGuid(), DocType = (DocType)Enum.Parse(typeof(DocType), finfo.Extension.Replace('.', ' ').Trim().ToUpper()), CreatedBy = Guid.Parse(CreatedBy), FileSizeInMB = (int)finfo.Length / 1024, FileType = finfo.Extension.Replace('.', ' ').Trim().ToUpper(), //not sure name = strFileName }; OrderHeaderDocumentBO orderBO = new OrderHeaderDocumentBO { Document = documentBO, OrderNo = DO }; DocumentDL dl = new DocumentDL(); dl.InsertDOHeaderDocument(orderBO); } return(Request.CreateResponse(HttpStatusCode.Created, new StringContent(" Files Uploaded Successfully"), Configuration.Formatters.JsonFormatter)); } catch (Exception ex) { //return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message, Configuration.Formatters.JsonFormatter); return(Request.CreateResponse(HttpStatusCode.InternalServerError, new StringContent("Upload Failed"), Configuration.Formatters.JsonFormatter)); } }