ASP.NET Core is Microsoft’s answer to open source web development platforms. Probably it was inevitable as they realize the futility of fighting the open source ecosystem with the ever growing popularity of node, npm, and bower. If you can’t beat them, join them 🙂eHealth Programmer Girl

Most healthcare organizations in Canada still use the Microsoft products and hence ASP.NET Core may be a good platform to build business applications. As it is platform agnostic, you can deploy it on Linux, if things change in the future (Yes, It is possible with Core). Of late I have been working on an ASP.Net Core project (an intranet portal) and would like to share some code snippets that may be useful to others.

If you need to export the CRUD Index (list) as .csv, here is a useful resource from @damienbod https://github.com/damienbod/AspNetCoreCsvImportExport

The implementation of the InputFormatter and the OutputFormatter classes are specific for a list of simple classes with only properties. If you have more complex classes, map only the properties that you need to serialize as below:

var libraries = _context.Libraries.Where(u => u.Contact.UserName == MyUserName.Name())
.Select(s => new { s.ContactID, s.RequestType, s.RequestFilledBy, s.RequestDate, s.RequestStatus, s.Pmid, s.Author, s.JournalBook, s.Title }).ToList();

I could not find any good reference to implement multiple file upload. The Microsoft’s documentation in this instance was unfortunately not very clear: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads

Here is my improvisation:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using System.IO;
using System.IO.Compression;

namespace MyNameSpace
{
    public static class FileUpload
    {
        public static byte[] ToZip(List<IFormFile> files)
        {
            long size = files.Sum(f => f.Length);

            // full path to file in temp location: var filePath = Path.GetTempFileName();
            var tempPath = Path.GetTempPath();
            var filePath = tempPath + "/submission/";
            var archiveFile = tempPath + "/zip/archive.zip";
            var archivePath = tempPath + "/zip/";
            if (Directory.Exists(filePath))
            {
                Directory.Delete(filePath, true);
            }
            if (Directory.Exists(archivePath))
            {
                Directory.Delete(archivePath, true);
            }

            Directory.CreateDirectory(filePath);
            Directory.CreateDirectory(archivePath);

            foreach (var formFile in files)
            {
                var fileName = filePath + formFile.FileName;
                if (formFile.Length > 0)
                {
                    using (var stream = new FileStream(fileName, FileMode.Create))
                    {
                        formFile.CopyToAsync(stream);
                    }
                }
            }
            ZipFile.CreateFromDirectory(filePath, archiveFile);
            /* beapen: 2017/07/24
             * 
             * Currently A Filestream cannot be directly converted to a byte array.
             * Hence it is copied to a memory stream before serializing it.
             * This may change in the future and may require refactoring.
             * 
             */
            var stream2 = new FileStream(archiveFile, FileMode.Open);
            var memoryStream = new MemoryStream();
            stream2.CopyTo(memoryStream);
            using (memoryStream)
            {
                return memoryStream.ToArray();
            }
        }
    }
}

 

https://gist.github.com/dermatologist/5f3900074e7383befe5363331de238e6

Hope this helps.

Follow Me