zoukankan      html  css  js  c++  java
  • juery upload

    <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>   





    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;
        }
    }
  • 相关阅读:
    input表单元素的默认padding不一致问题
    【转】iOS25彩票 幸运转盘
    微分起源
    转载--微分几何为何必然兴起?
    前缀和?or差分序列?
    noip2014 小结
    2019艾瑞(北京)年度高峰会议-数能驱动新变量
    MSF初体验
    s实现指定时间自动跳转到某个页面
    css实现居中
  • 原文地址:https://www.cnblogs.com/lovewl2/p/11304128.html
Copyright © 2011-2022 走看看