温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

.net core 上传excel文件

发布时间:2020-05-29 07:24:36 来源:网络 阅读:3022 作者:pangshang 栏目:编程语言

引用:
using System.Net.Http.Headers;


依赖注入:

private readonly IHostingEnvironment hostingEnvironment;
public HomeController(IHostingEnvironment env)
{
      this.hostingEnvironment = env;
}

cshtml 使用 h-ui admin模板(若不需要直接上个 file标签就行):

去掉 file标签中的 accept 限制 可以传各种文件

        <form asp-controller="Home" role="form" asp-action="ImportExcel" enctype="multipart/form-data" method="post"
              class="form form-horizontal" id="form-add">
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>选择Excel <span >xlsx</span> 导入:</label>
                <div class="formControls col-xs-8 col-sm-9">
                    @*//带文本框*@
                    <span class="btn-upload form-group">
                        <input class="input-text upload-url radius" type="text" name="uploadfile" id="uploadfile" readonly>
                        <a href="javascript:void();" class="btn btn-primary radius">
                            浏览文件
                        </a>
                        <input type="file" multiple name="fileinput" class="input-file" accept=".xlsx" />
                    </span>
                </div>
            </div>
            <div class="row cl">
                <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                    <input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
                </div>
            </div>
        </form>

Action:

   [HttpPost]
    public async Task<IActionResult> ImportExcel(IFormFile fileinput)
    {
            try
            {
                var filename = ContentDispositionHeaderValue.Parse(fileinput.ContentDisposition).FileName; // 原文件名(包括路径)
                var extName = filename.Substring(filename.LastIndexOf('.')).Replace("\"", "");// 扩展名
                string shortfilename = $"{Guid.NewGuid()}{extName}";// 新文件名
                string fileSavePath = hostingEnvironment.WebRootPath + @"\upload\";//文件临时目录,导入完成后 删除
                filename = fileSavePath + shortfilename; // 新文件名(包括路径)
                if (!Directory.Exists(fileSavePath))
                {
                    Directory.CreateDirectory(fileSavePath);
                }
                using (FileStream fs = System.IO.File.Create(filename)) // 创建新文件
                {
                    fileinput.CopyTo(fs);// 复制文件
                    fs.Flush();// 清空缓冲区数据
                    //根据 filename 【文件服务器磁盘路径】可对文件进行业务操作
                }
                //处理完成后,删除上传的文件
                if (System.IO.File.Exists(filename))
                {
                    System.IO.File.Delete(filename);
                }
                return new JsonResult(importResult);
            }
            catch (Exception ex)
            {

            }
    }
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI