Search code examples
jqueryasp.net-mvcfile-ioasp.net-ajaxexceldatareader

How to read excel from ajax to web api


Here's my ajax call:

 $.ajax({
                    type: "POST",
                    url: "/api/excels/Upload",
                    data: $file,
                    contentType: false,
                    processData: false
                }).done(function (result) {
                    alert(result);
                }).fail(function (a, b, c) {
                    console.log(a, b, c);
                });
                return false;// if it's a link to prevent post
            });

Here's my Web APi Controller function:

 [HttpPost]
        public async Task<string> Upload()
        {

            FilePath filePath = "HOW SHOULD I GET THE FILE???"; 
            FileStream stream = System.IO.File.Open(filePath, FileMode.Open, FileAccess.Read);
            IExcelDataReader excelReader = null;

            //Check file type 
            if (filePath.EndsWith(".xls"))
            {
                excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
            }

            else if (filePath.EndsWith(".xlsx"))
            {
                excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
            }

            var result = excelReader.AsDataSet(new ExcelDataSetConfiguration()
            {
                ConfigureDataTable = (_) => new ExcelDataTableConfiguration()
                {
                    UseHeaderRow = true
                }
            });

            String sheetName = result.Tables[0].TableName;

        }

As you can see, I am using ExcelReader to get the file and parse it to read it's contents. My end goal is to read each row and save each row in DB using Entity Framework. I cannot use Form to access the file either because it's Web Api. Will appreciate your help.


Solution

  • First, to upload the file, you can use a view with code like this:

    @section scripts
    {
    <script type="text/javascript">
        $(document).ready(function() {
            $('#upload').click(function () {
                var data = new FormData();
                var file = $('form input[type=file]')[0].files[0];
                data.append('file',file);
                $.ajax({
                    url: '/api/excels/Upload',
                    processData: false,
                    contentType: false,
                    data: data,
                    type: 'POST'
                }).done(function(result) {
                    alert(result);
                }).fail(function(a, b, c) {
                    console.log(a, b, c);
                });
            });
        });
    </script>    
    }
    

    Second, to receive data and chack, change method like this:

    public class ExcelsController : ApiController
    {
        [HttpPost]
        public async Task<string> Upload()
        {
           var provider = new MultipartMemoryStreamProvider();
           await Request.Content.ReadAsMultipartAsync(provider);
    
           // extract file name and file contents
           Stream stream = new MemoryStream(await provider.Contents[0].ReadAsByteArrayAsync());
    
           //get fileName
           var filename = provider.Contents[0].Headers.ContentDisposition.FileName.Replace("\"", string.Empty);
    
            //Check file type 
            if (filename .EndsWith(".xls"))
            {
                excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
            }
    
            else if (filename .EndsWith(".xlsx"))
            {
                excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
            }
            else
            {
                return "Not Valid";
            }
            var result = excelReader.AsDataSet(new ExcelDataSetConfiguration()
            {
                ConfigureDataTable = (_) => new ExcelDataTableConfiguration()
                {
                    UseHeaderRow = true
                }
            });
    
    
           return result;
        }
    }