.netcore上傳excel文件

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

創(chuàng)新互聯(lián)是一家專業(yè)提供忻府企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、HTML5、小程序制作等業(yè)務(wù)。10年已為忻府眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。


依賴注入:

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

cshtml 使用 h-ui admin模板(若不需要直接上個(gè) file標(biāo)簽就行):

去掉 file標(biāo)簽中的 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> 導(dǎo)入:</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("\"", "");// 擴(kuò)展名
                string shortfilename = $"{Guid.NewGuid()}{extName}";// 新文件名
                string fileSavePath = hostingEnvironment.WebRootPath + @"\upload\";//文件臨時(shí)目錄,導(dǎo)入完成后 刪除
                filename = fileSavePath + shortfilename; // 新文件名(包括路徑)
                if (!Directory.Exists(fileSavePath))
                {
                    Directory.CreateDirectory(fileSavePath);
                }
                using (FileStream fs = System.IO.File.Create(filename)) // 創(chuàng)建新文件
                {
                    fileinput.CopyTo(fs);// 復(fù)制文件
                    fs.Flush();// 清空緩沖區(qū)數(shù)據(jù)
                    //根據(jù) filename 【文件服務(wù)器磁盤路徑】可對(duì)文件進(jìn)行業(yè)務(wù)操作
                }
                //處理完成后,刪除上傳的文件
                if (System.IO.File.Exists(filename))
                {
                    System.IO.File.Delete(filename);
                }
                return new JsonResult(importResult);
            }
            catch (Exception ex)
            {

            }
    }

新聞標(biāo)題:.netcore上傳excel文件
本文地址:http://muchs.cn/article14/gepjde.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、小程序開發(fā)、動(dòng)態(tài)網(wǎng)站、關(guān)鍵詞優(yōu)化、軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)