public async Task <IActionResult> UploadContract([FromBody] UploadContractViewModel request) { if (!request.Bytes.Any()) { return(StatusCode(500, new BaseResult <object> { IsError = true, Message = "bytes field is required" })); } var uploadContractResult = await iDocumentLibrary.UploadContract(request).ConfigureAwait(false); if (uploadContractResult.IsError && uploadContractResult.ExceptionMessage != null) { return(StatusCode(500, uploadContractResult)); } return(Ok(uploadContractResult)); }
public async Task <BaseResult <string> > UploadContract(UploadContractViewModel request) { try { var mainDirectory = "\\MGDocuments"; if (!Directory.Exists(DocumentStorePath + mainDirectory)) { Directory.CreateDirectory(DocumentStorePath + mainDirectory); } var hotelDirectory = mainDirectory + "\\hotelId-" + request.HotelID.Value; if (!Directory.Exists(DocumentStorePath + hotelDirectory)) { Directory.CreateDirectory(DocumentStorePath + hotelDirectory); } var contractDirectory = hotelDirectory + "\\contracts"; if (!Directory.Exists(DocumentStorePath + contractDirectory)) { Directory.CreateDirectory(DocumentStorePath + contractDirectory); } var fileToUpload = contractDirectory + "\\" + request.Id.Value + ".pdf"; //File.Delete(DocumentStorePath + fileToUpload); using (var fileStream = new FileStream(DocumentStorePath + fileToUpload, FileMode.OpenOrCreate)) { var bytes = request.Bytes.ToArray(); fileStream.Flush(); await fileStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false); } return(new BaseResult <string>() { Result = fileToUpload }); } catch (Exception e) { return(new BaseResult <string>() { IsError = true, ExceptionMessage = e }); } }