public IHttpActionResult CreateFromResult([FromBody] ResultFileUploadModel model) { var userId = Request.GetUserId(); if (userId < 0) { return(Unauthorized()); } if (model == null) { return(BadRequest("file is null")); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var fileName = $"{Guid.NewGuid():N}.{model.Extension}"; var directory = Path.Combine(HttpRuntime.AppDomainAppPath, FileManager.RELATIVE_PATH); var directoryThumbnail = Path.Combine(HttpRuntime.AppDomainAppPath, FileManager.RELATIVE_PATH_THUMBNAIL); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } if (!Directory.Exists(directoryThumbnail)) { Directory.CreateDirectory(directoryThumbnail); } var filePath = Path.Combine(directory, fileName); var filePathThumbnail = Path.Combine(directoryThumbnail, fileName); System.IO.File.WriteAllText(filePath, model.FileContent); //var image = Image.FromFile(filePath); //var thumbnail = (Image)new Bitmap(image, new Size(200, 200)); //thumbnail.Save(filePathThumbnail); var urlPath = GetUrlPath(FileManager.RELATIVE_PATH, fileName); var urlPathThumbnail = GetUrlPath(FileManager.RELATIVE_PATH_THUMBNAIL, fileName); var fileInfo = new FileInfo(filePath); var file = new File { Extension = model.Extension, FilePath = urlPath, ThumbnailPath = urlPathThumbnail, Size = (int)(fileInfo.Length / 1024), RowState = RowState.Created, UploadedAt = DateTime.Now, UploadedBy = userId }; _fileManager.Add(file); return(Ok(Mapper.Map <FileViewModel>(file))); }
public IHttpActionResult CreateFromBase64([FromBody] ResultFileUploadModel model) { var userId = Request.GetUserId(); if (userId < 0) { return(Unauthorized()); } if (model == null) { return(BadRequest("file is null")); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var strSplit = model.FileContent.Split(new[] { "base64," }, StringSplitOptions.None); if (strSplit.Length != 2) { return(BadRequest("invalid base64")); } var base64Str = strSplit[1]; var fileName = $"{Guid.NewGuid():N}.{model.Extension}"; var directory = Path.Combine(HttpRuntime.AppDomainAppPath, FileManager.RELATIVE_PATH); var directoryThumbnail = Path.Combine(HttpRuntime.AppDomainAppPath, FileManager.RELATIVE_PATH_THUMBNAIL); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } if (!Directory.Exists(directoryThumbnail)) { Directory.CreateDirectory(directoryThumbnail); } var filePath = Path.Combine(directory, fileName); var filePathThumbnail = Path.Combine(directoryThumbnail, fileName); var bytes = Convert.FromBase64String(base64Str); using (var imageFile = new FileStream(filePath, FileMode.Create)) { imageFile.Write(bytes, 0, bytes.Length); imageFile.Flush(); } var image = Image.FromFile(filePath); var thumbnail = (Image) new Bitmap(image, new Size(200, 200)); thumbnail.Save(filePathThumbnail); var urlPath = GetUrlPath(FileManager.RELATIVE_PATH, fileName); var urlPathThumbnail = GetUrlPath(FileManager.RELATIVE_PATH_THUMBNAIL, fileName); var fileInfo = new FileInfo(filePath); var file = new File { Extension = model.Extension, FilePath = urlPath, ThumbnailPath = urlPathThumbnail, Size = (int)(fileInfo.Length / 1024), RowState = RowState.Created, UploadedAt = DateTime.Now, UploadedBy = userId, Name = fileName }; _fileManager.Add(file); return(Ok(Mapper.Map <FileViewModel>(file))); }