Exemple #1
0
        public async Task UpdateFiles(Employee item, IQueryDictionary <IFileListEntry[]> files)
        {
            if (files.Count > 0)
            {
                var file = files.FirstOrDefault();
                if (file.Value.Length > 0)
                {
                    var ms = new MemoryStream();
                    await file.Value[0].Data.CopyToAsync(ms);
                    byte[] ba = new byte[ms.Length + 78];
                    for (int i = 0; i < 78; i++)
                    {
                        ba[i] = 0;
                    }
                    Array.Copy(ms.ToArray(), 0, ba, 78, ms.Length);
                    item.Photo = ba;

                    using (var context = new NorthwindDbContext(_options))
                    {
                        try
                        {
                            var repository = new EmployeeRepository(context);
                            await repository.Update(item);

                            repository.Save();
                        }
                        catch (Exception e)
                        {
                            throw new GridException(e);
                        }
                    }
                }
            }
        }
Exemple #2
0
        public async Task <Employee> UpdateFiles(Employee item, IQueryDictionary <IFileListEntry[]> files)
        {
            if (files.Count > 0)
            {
                var file = files.FirstOrDefault();
                if (file.Value.Length > 0)
                {
                    // add OLE header to file data byte array
                    var ms = new MemoryStream();
                    await file.Value[0].Data.CopyToAsync(ms);
                    byte[] ba = new byte[ms.Length + 78];
                    for (int i = 0; i < 78; i++)
                    {
                        ba[i] = 0;
                    }
                    Array.Copy(ms.ToArray(), 0, ba, 78, ms.Length);

                    // convert byte array to url scaped base64
                    string base64Str = Convert.ToBase64String(ba);
                    base64Str = base64Str.Replace('+', '.');
                    base64Str = base64Str.Replace('/', '_');
                    base64Str = base64Str.Replace('=', '-');

                    item.Base64String = base64Str;
                    var response = await _httpClient.PostAsJsonAsync(_baseUri + $"api/SampleData/SetEmployeePhoto", item);

                    if (!response.IsSuccessStatusCode)
                    {
                        throw new GridException("EMPSRV-04", "Error updating the employee");
                    }
                }
            }
            return(item);
        }
        public async Task <Employee> UpdateFiles(Employee item, IQueryDictionary <IFileListEntry[]> files)
        {
            if (files.Count > 0)
            {
                var file = files.FirstOrDefault();
                if (file.Value.Length > 0)
                {
                    // add OLE header to file data byte array
                    var ms = new MemoryStream();
                    await file.Value[0].Data.CopyToAsync(ms);
                    byte[] ba = new byte[ms.Length + 78];
                    for (int i = 0; i < 78; i++)
                    {
                        ba[i] = 0;
                    }
                    Array.Copy(ms.ToArray(), 0, ba, 78, ms.Length);
                    item.Photo = ba;

                    await _employeeService.Update(item);
                }
            }
            return(item);
        }