webapi 上传excel或者txt

前台文件上传两种方式:

1、form表单提交;此处enctype必须填写,否则后台获取不到

<form action="http://www.baidu.com/api/CardFX/UploadGameAccount" method="post" enctype="multipart/form-data">
  <input type="file" name="file" value="选择文件" />
  <input id="submit_form" type="submit" class="btn btn-success save" value="上传" />
</form>

2、ajaxupload.js上传;

<script src="/js/ajaxupload.js" type="text/javascript"></script>
<script type="text/javascript">
  $(document).ready(function () {
            reloadUpFiles();
            new AjaxUpload('#buttonUpload1', {
                action: '../Ashx/AddImages.ashx',
                data: { Folder: '文件路径', CommodityNo: '文件名' },
                onSubmit: function (file, ext) {
                    $("#loading1").show();
                    if (!/^(jpg|bmp|gif|png)$/.test(ext)) {
                        alert("只能上传格式为(bmp、jpg、gif、png)格式的图片!");
                        $("#loading1").hide();
                        return false;
                    }
                    this.disable();
                },
                onComplete: function (file, response) {
                    
                }
            })
        });
</script>

后台C#代码:

     /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public List<T_DTO_CardOrder.GameAccountResponse> UploadGameAccount()
        {
            HttpPostedFile file = HttpContext.Current.Request.Files[0];
            string fileType = Path.GetExtension(file.FileName);
            Stream s = file.InputStream;
            DataTable dt = new DataTable();
            try
            {
                if (fileType.ToLower() == ".txt")
                {
                    dt.Columns.Add("gameAccount");
                    StreamReader sr = new StreamReader(s, Encoding.Default);
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (!string.IsNullOrWhiteSpace(line))
                        {
                            DataRow dr = dt.NewRow();
                            dr["gameAccount"] = line;
                            dt.Rows.Add(dr);
                        }
                    }
                }
                else
                {
                    dt = ImportExcel(s);
                }
            }
            catch (Exception ex)
            {
                C_Log.LogException(ex);
            }
            List<T_DTO_CardOrder.GameAccountResponse> model = new List<T_DTO_CardOrder.GameAccountResponse>();
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    model.Add(new T_DTO_CardOrder.GameAccountResponse()
                    {
                        gameAccount = dr[0].ToString()
                    });
                }
            }
            return model;
        }

 

posted @ 2018-12-28 13:34  木子zzgxl  阅读(400)  评论(0编辑  收藏  举报