public async Task <FileViewModel> DownloadFile(int ProductId) { var product = _productsRepository.getProductbyId(ProductId); if (product != null) { var productSavedFile = _productsRepository.GetProuctDataSheet(ProductId); try { using (var client = new HttpClient()) { using (var result = await client.GetAsync(product.Url)) { if (result.IsSuccessStatusCode) { var FileInfo = new FileViewModel(); FileInfo.content = await result.Content.ReadAsByteArrayAsync(); FileInfo.ContentType = result.Content.Headers.ContentType.MediaType; FileInfo.LastModified = result.Content.Headers.LastModified.HasValue ? (DateTime?)result.Content.Headers.LastModified.Value.DateTime : null; if (productSavedFile == null) { productSavedFile = new ProductDataSheets { Content = FileInfo.content, ContentType = FileInfo.ContentType, LastModified = FileInfo.LastModified, ProductId = ProductId, }; _productsRepository.AddProuctDataSheet(productSavedFile); } else if (productSavedFile.LastModified != FileInfo.LastModified) { productSavedFile.Content = FileInfo.content; productSavedFile.ContentType = FileInfo.ContentType; productSavedFile.LastModified = FileInfo.LastModified; _productsRepository.Commit(); } return(FileInfo); } else { product.DocumentAvaliable = false; _productsRepository.Commit(); } } } } catch (Exception ex) { product.DocumentAvaliable = false; _productsRepository.Commit(); Log.Error(ex, "Could not get file data"); } if (productSavedFile != null) { return(new FileViewModel { content = productSavedFile.Content, ContentType = productSavedFile.ContentType, LastModified = productSavedFile.LastModified }); } } return(null); }