public async Task <IActionResult> UploadFile(IFormFile file, string accountUrl, string container, string prefix = null) { var response = new JsonResult <FileUploadResult>() { Success = false, Result = new FileUploadResult() }; if (!GenericCheck(() => _TokenService.HasToken, response, StatusCodes.Status401Unauthorized, SiteErrorCodes.NotAuthorized, () => Url.RouteUrl(Routes.GET_Home_Route))) { return(Json(response)); } var(status, newFile) = await _UploadService.AddFile(file, accountUrl, container, prefix); if (status != UploadService.UploadStatus.Ok) { response.Success = false; (response.StatusCode, response.ErrorCode) = ErrorCodes.FromUploadStatus(status); return(JsonResult(response)); } response.Success = true; response.StatusCode = StatusCodes.Status200OK; //response.Result.FileId = newFile.Id; response.Result.FileName = newFile.FileName; response.Result.SizeKB = (int)(newFile.Size / 1024); return(JsonResult(response)); }
public async Task <HttpResponseMessage> PostFormData() { // Check if the request contains multipart/form-data. if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } var companyDir = Path.Combine(BaseFilePath, CompanyId.ToString()); if (!Directory.Exists(companyDir)) { Directory.CreateDirectory(companyDir); } var provider = new CustomMultipartFormDataStreamProvider(companyDir); try { // Read the form data. await Request.Content.ReadAsMultipartAsync(provider); var soaChapterId = provider.FormData.GetValues("soachapterid")[0]; var soaType = int.Parse(provider.FormData.GetValues("soaType")[0]); var fileName = provider.FileData[0].Headers.ContentDisposition.FileName.Replace("\"", "").Trim(); var master = bool.Parse(provider.FormData.GetValues("master")[0]); if (soaType == 3 && master) { var masterCompanyDir = Path.Combine(BaseFilePath, "000"); if (!Directory.Exists(masterCompanyDir)) { Directory.CreateDirectory(masterCompanyDir); } File.Move(Path.Combine(companyDir, provider.FileData[0].LocalFileName), Path.Combine(masterCompanyDir, provider.FileData[0].LocalFileName)); companyDir = masterCompanyDir; } var chapterDir = Path.Combine(companyDir, soaChapterId); if (!Directory.Exists(chapterDir)) { Directory.CreateDirectory(chapterDir); } fileName = Path.Combine(chapterDir, fileName); if (File.Exists(fileName)) { File.Delete(fileName); } File.Move(Path.Combine(companyDir, provider.FileData[0].LocalFileName), fileName); //Update db with filenames var file = UploadService.AddFile(Convert.ToInt32(soaChapterId), fileName, soaType == 3 && master); return(Request.CreateResponse(HttpStatusCode.OK, file)); } catch (System.Exception e) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e)); } }